drv_i2c_main.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386
  1. #include <linux/version.h>
  2. #include <asm/mipsregs.h>
  3. #include <drv_devices.h>
  4. #include <drv_platform.h>
  5. #include <drv_i2c.h>
  6. #include <linux/slab.h> //20100915 lwhite add this for porting driver to Kernel-2.6.35 (for drv_kmalloc & drv_vfree)
  7. #include "drv_i2c_main.h"
  8. I2C_DEV I2cDev;
  9. struct list_head CmdList;
  10. UINT32 M0BusyCounter = 0;
  11. UINT32 M1BusyCounter = 0;
  12. UINT8 bSuspend = 0;
  13. UINT16 g_ucHangNum = 0;
  14. UINT16 g_BusyLoop = 100;
  15. #define I2C_STATUS_SYSTEM_TIMEOUT 6
  16. #define GPIO_I2C
  17. #if 1
  18. //330 did ot supprt suspend/resume
  19. #else
  20. INT32 I2CResume(void);
  21. INT32 I2CSuspend(pm_message_t state);
  22. #endif
  23. INT32 DRV_I2C_QueueCmdFun(UINT8 Mx,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed, INT8 bRead);
  24. INT32 DRV_I2C_RunCmdFun(UINT8 Mx,UINT8 *pData);
  25. void RunQueueFun(struct work_struct *work);
  26. void MxI2CRestart(UINT8 Mx,UINT32 lag);
  27. void MxI2CStart(UINT8 Mx,UINT32 lag);
  28. void MxI2CStop(UINT8 Mx,UINT32 lag);
  29. void MxI2CWriteByte(UINT8 Mx,UINT8 Value,UINT32 lag);
  30. UINT32 SwI2CReadBasicFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 BaseAddress,UINT8 Length,UINT8 Speed,UINT8 *pStatus);
  31. void SwI2CWriteBasicFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 BaseAddress,UINT32 Data,UINT8 Length,UINT8 Speed,UINT8 *pStatus);
  32. void I2CRead(UINT8 MasterIndex,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed,UINT8 *pStatus);
  33. void I2CWrite(UINT8 MasterIndex,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed,UINT8 *pStatus);
  34. static INT32 QueueI2CFun(UINT8 Mx,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed)
  35. {
  36. unsigned long flags;
  37. PQUEUE_I2C pQueue;
  38. pQueue = drv_kmalloc(sizeof(QUEUE_I2C)+Length-1,GFP_ATOMIC, MODULEID_I2C); // Cannot sleep
  39. if(!pQueue)
  40. {
  41. DebugPrint("Cannot allocate QUEUE_I2C");
  42. return -ENOMEM;
  43. }
  44. // INIT_LIST_HEAD(&pQueue->list);
  45. pQueue->Mx = Mx;
  46. pQueue->DeviceID = DeviceID;
  47. pQueue->AddrType = AddrType;
  48. pQueue->Speed = Speed;
  49. pQueue->Address = Address;
  50. pQueue->Length = Length;
  51. memcpy(pQueue->Data,pData,Length);
  52. spin_lock_irqsave(&I2cDev.Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
  53. list_add_tail(&pQueue->list,&I2cDev.pQueueWriteList);
  54. spin_unlock_irqrestore(&I2cDev.Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
  55. queue_work(I2cDev.i2cWorkQueue,&I2cDev.QueueWork);
  56. DebugPrint("WARNING! Queue Mx=%d, ID=%02x, Type=%d, Addr=%08x, Len=%d, Speed=%d"
  57. ,Mx,DeviceID,AddrType,Address,Length,Speed);
  58. return 0;
  59. }
  60. /*
  61. void RunQueueFun(void *Context)
  62. {
  63. struct list_head QueueList,*cursor,*next;
  64. UINT32 flags;
  65. PQUEUE_I2C pQueue;
  66. UINT8 Status;
  67. INIT_LIST_HEAD(&QueueList);
  68. spin_lock_irqsave(&I2cDev.Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
  69. list_splice_init(&I2cDev.pQueueWriteList,&QueueList);
  70. spin_unlock_irqrestore(&I2cDev.Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
  71. list_for_each_safe(cursor,next,&QueueList)
  72. {
  73. list_del(cursor);
  74. pQueue = container_of(cursor,QUEUE_I2C,list);
  75. Status = I2C_STATUS_SUCCESS;
  76. // DebugPrint("RunQueue Mx=%d, ID=%02x, Type=%d, Addr=%08x, Len=%d, Speed=%d"
  77. // ,pQueue->Mx,pQueue->DeviceID,pQueue->AddrType
  78. // ,pQueue->Address,pQueue->Length,pQueue->Speed);
  79. DRV_I2C_WriteFun(pQueue->Mx
  80. ,pQueue->DeviceID
  81. ,pQueue->AddrType
  82. ,pQueue->Address
  83. ,pQueue->Data,pQueue->Length
  84. ,pQueue->Speed,&Status);
  85. drv_kfree(pQueue, MODULEID_I2C);
  86. }
  87. }
  88. */
  89. //20100209 lwhite modified for new kernel
  90. void RunQueueFun(struct work_struct *work)
  91. {
  92. PI2C_DEV pI2cDev = container_of(work, I2C_DEV, QueueWork);
  93. struct list_head QueueList,*cursor = NULL,*next = NULL;
  94. unsigned long flags;
  95. UINT8 Status;
  96. INIT_LIST_HEAD(&QueueList);
  97. spin_lock_irqsave(&pI2cDev->Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
  98. list_splice_init(&pI2cDev->pQueueWriteList,&QueueList);
  99. spin_unlock_irqrestore(&pI2cDev->Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
  100. list_for_each_safe(cursor,next,&QueueList)
  101. {
  102. PQUEUE_I2C pQueue;
  103. list_del(cursor);
  104. pQueue = container_of(cursor,QUEUE_I2C,list);
  105. Status = I2C_STATUS_SUCCESS;
  106. // DebugPrint("RunQueue Mx=%d, ID=%02x, Type=%d, Addr=%08x, Len=%d, Speed=%d"
  107. // ,pQueue->Mx,pQueue->DeviceID,pQueue->AddrType
  108. // ,pQueue->Address,pQueue->Length,pQueue->Speed);
  109. DRV_I2C_WriteFun(pQueue->Mx
  110. ,pQueue->DeviceID
  111. ,pQueue->AddrType
  112. ,pQueue->Address
  113. ,pQueue->Data,pQueue->Length
  114. ,pQueue->Speed,&Status);
  115. drv_kfree(pQueue, MODULEID_I2C);
  116. }
  117. }
  118. #if SOFTWARE_MODE_MASTER
  119. #define newdelay(time2) {\
  120. UINT32 TargetA,curA,refA;\
  121. MmioReadFun(M0_CONTROL0);\
  122. refA = read_c0_count();\
  123. TargetA = (time2)*100 + refA;\
  124. if(TargetA<refA)\
  125. {\
  126. do\
  127. {\
  128. udelay(1);\
  129. curA = read_c0_count();\
  130. }while(curA>refA||(curA<refA && curA<TargetA));\
  131. }\
  132. else\
  133. {\
  134. do\
  135. {\
  136. udelay(1);\
  137. curA = read_c0_count();\
  138. }while(curA<TargetA && curA>refA);\
  139. }\
  140. }
  141. #define WaitClk(time,hl) {\
  142. UINT32 limit;\
  143. if(hl==1)\
  144. {\
  145. for(limit=0;limit<12 && (SW_MASTER_SCL_IN&MmioReadWordFun(Control))==0;limit++)\
  146. {\
  147. udelay(1);\
  148. }\
  149. }\
  150. else\
  151. {\
  152. for(limit=0;limit<12 && (SW_MASTER_SCL_IN&MmioReadWordFun(Control))!=0;limit++)\
  153. {\
  154. udelay(1);\
  155. }\
  156. }\
  157. if(time>limit)\
  158. newdelay(time-limit);\
  159. }
  160. void MxI2CStart(UINT8 Mx,UINT32 lag)
  161. {
  162. PVOID Control;
  163. UINT16 Data;
  164. if(Mx==0)
  165. {
  166. Control = (PVOID)SW_MASTER0;
  167. }
  168. else
  169. {
  170. Control = (PVOID)SW_MASTER1;
  171. }
  172. Data = MmioReadWordFun(Control);
  173. Data |= SW_MASTER_ENABLE | SW_MASTER_SCL_OUT_OEN | SW_MASTER_SDA_OUT_OEN;
  174. MmioWriteWordFun(Control,Data); // Data:1, Clk:1
  175. newdelay(2);
  176. Data &= ~SW_MASTER_SDA_OUT_OEN;
  177. MmioWriteWordFun(Control,Data); // Data:0, Clk:1
  178. newdelay(lag);
  179. Data &= ~SW_MASTER_SCL_OUT_OEN;
  180. MmioWriteWordFun(Control,Data); // Data:0, Clk:0
  181. WaitClk(lag,0);
  182. I2cDev.Status[Mx] = I2C_STATUS_SUCCESS;
  183. }
  184. void MxI2CStop(UINT8 Mx,UINT32 lag)
  185. {
  186. PVOID Control;
  187. UINT16 Data;
  188. if(Mx==0)
  189. {
  190. Control = (PVOID)SW_MASTER0;
  191. }
  192. else
  193. {
  194. Control = (PVOID)SW_MASTER1;
  195. }
  196. Data = MmioReadWordFun(Control);
  197. Data &= ~(SW_MASTER_SDA_OUT_OEN | SW_MASTER_SCL_OUT_OEN);
  198. MmioWriteWordFun(Control,Data); // Data:0, Clk:0
  199. newdelay(lag);
  200. Data |= SW_MASTER_SCL_OUT_OEN;
  201. MmioWriteWordFun(Control,Data); // Data:0, Clk:1
  202. WaitClk(lag,1);
  203. Data |= SW_MASTER_SDA_OUT_OEN;
  204. MmioWriteWordFun(Control,Data); // Data:1, Clk:1
  205. newdelay(lag);
  206. Data &= ~SW_MASTER_ENABLE;
  207. MmioWriteWordFun(Control,Data);
  208. newdelay(2);
  209. }
  210. void MxI2CRestart(UINT8 Mx,UINT32 lag)
  211. {
  212. PVOID Control;
  213. UINT16 Data;
  214. if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
  215. return;
  216. if(Mx==0)
  217. {
  218. Control = (PVOID)SW_MASTER0;
  219. }
  220. else
  221. {
  222. Control = (PVOID)SW_MASTER1;
  223. }
  224. Data = MmioReadWordFun(Control);
  225. Data &= ~SW_MASTER_SCL_OUT_OEN;
  226. Data |= SW_MASTER_SDA_OUT_OEN;
  227. MmioWriteWordFun(Control,Data); // Data:1, Clk:0
  228. newdelay(lag);
  229. Data |= SW_MASTER_SCL_OUT_OEN;
  230. MmioWriteWordFun(Control,Data); // Data:1, Clk:1
  231. WaitClk(lag,1);
  232. Data &= ~SW_MASTER_SDA_OUT_OEN;
  233. MmioWriteWordFun(Control,Data); // Data:0, Clk:1
  234. newdelay(lag);
  235. Data &= ~SW_MASTER_SCL_OUT_OEN;
  236. MmioWriteWordFun(Control,Data); // Data:0, Clk:0
  237. WaitClk(lag,0);
  238. }
  239. void MxI2CWriteByte(UINT8 Mx,UINT8 Value,UINT32 lag)
  240. {
  241. PVOID Control;
  242. UINT16 Data,i;
  243. if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
  244. return;
  245. if(Mx==0)
  246. {
  247. Control = (PVOID)SW_MASTER0;
  248. }
  249. else
  250. {
  251. Control = (PVOID)SW_MASTER1;
  252. }
  253. Data = MmioReadWordFun(Control);
  254. /* Data &= ~SW_MASTER_SCL_OUT_OEN;
  255. MmioWriteWordFun(Control,Data); // Data:x, Clk:0
  256. newdelay(lag);*/
  257. for(i=0;i<8;i++)
  258. {
  259. if(Value & 0x80)
  260. {
  261. Data |= SW_MASTER_SDA_OUT_OEN;
  262. }
  263. else
  264. {
  265. Data &= ~SW_MASTER_SDA_OUT_OEN;
  266. }
  267. MmioWriteWordFun(Control,Data); // Data:x, Clk:0
  268. newdelay(lag);
  269. Data ^= SW_MASTER_SCL_OUT_OEN;
  270. MmioWriteWordFun(Control,Data); // Data:x, Clk:1
  271. WaitClk(lag,1);
  272. Data ^= SW_MASTER_SCL_OUT_OEN;
  273. MmioWriteWordFun(Control,Data); // Data:x, Clk:0
  274. WaitClk(lag,0);
  275. Value <<= 1;
  276. }
  277. Data |= SW_MASTER_SDA_OUT_OEN;
  278. MmioWriteWordFun(Control,Data); // Data:1, Clk:0
  279. newdelay(lag);
  280. for(i=0;i<8 && (SW_MASTER_SDA_IN & MmioReadWordFun(Control))!=0;i++)
  281. udelay(1);
  282. Data ^= SW_MASTER_SCL_OUT_OEN;
  283. MmioWriteWordFun(Control,Data); // Data:ACK, Clk:1
  284. WaitClk(lag,1);
  285. if(SW_MASTER_SDA_IN & MmioReadWordFun(Control))
  286. {
  287. I2cDev.Status[Mx] = I2C_STATUS_DATA_ERROR;
  288. }
  289. Data ^= SW_MASTER_SCL_OUT_OEN;
  290. MmioWriteWordFun(Control,Data); // Data:1, Clk:0
  291. WaitClk(lag,0);
  292. }
  293. UINT8 MxI2CReadByte(UINT8 Mx,UINT32 lag,UINT8 bMoreByte)
  294. {
  295. PVOID Control;
  296. UINT16 Data,i;
  297. UINT8 Value=0;
  298. if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
  299. return -1;
  300. if(Mx==0)
  301. {
  302. Control = (PVOID)SW_MASTER0;
  303. }
  304. else
  305. {
  306. Control = (PVOID)SW_MASTER1;
  307. }
  308. Data = MmioReadWordFun(Control) | SW_MASTER_SDA_OUT_OEN;
  309. Data &= ~SW_MASTER_SCL_OUT_OEN;
  310. MmioWriteWordFun(Control,Data); // Data:1, Clk:0
  311. newdelay(lag);
  312. for(i=0;i<8;i++)
  313. {
  314. Value <<= 1;
  315. Data ^= SW_MASTER_SCL_OUT_OEN;
  316. MmioWriteWordFun(Control,Data); // Data:1, Clk:1
  317. WaitClk(lag,1);
  318. if(SW_MASTER_SDA_IN & MmioReadWordFun(Control))
  319. {
  320. Value |= 1;
  321. }
  322. Data ^= SW_MASTER_SCL_OUT_OEN;
  323. MmioWriteWordFun(Control,Data); // Data:1, Clk:0
  324. WaitClk(lag,0);
  325. }
  326. if(bMoreByte)
  327. {
  328. Data &= ~SW_MASTER_SDA_OUT_OEN;
  329. MmioWriteWordFun(Control,Data); // Data:0, Clk:0
  330. newdelay(lag);
  331. }
  332. Data ^= SW_MASTER_SCL_OUT_OEN;
  333. MmioWriteWordFun(Control,Data); // Data:x, Clk:1
  334. WaitClk(lag,1);
  335. Data ^= SW_MASTER_SCL_OUT_OEN;
  336. MmioWriteWordFun(Control,Data); // Data:x, Clk:0
  337. WaitClk(lag,0);
  338. return Value;
  339. }
  340. #if 0
  341. UINT32 SwI2CReadBasicFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 BaseAddress,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
  342. {
  343. //UINT32 Data,i;
  344. UINT32 Data;
  345. UINT8 i;
  346. UINT32 lag;
  347. UINT8 *pData;
  348. if(Length>4)
  349. {
  350. *pStatus = I2C_STATUS_DATA_ERROR;
  351. DbgFunPrint("Invalid Parameter");
  352. return -1;
  353. }
  354. if(in_atomic())
  355. {
  356. DbgFunPrint("ID=%02x Addr=%02x,Invalid Process!!",SlaveAddress,BaseAddress);
  357. *pStatus = I2C_STATUS_DATA_ERROR;
  358. return -1;
  359. }
  360. if(Speed==I2C_SPEED_100K)
  361. {
  362. lag = SW_MASTER_100K;
  363. }
  364. else
  365. {
  366. lag = SW_MASTER_400K;
  367. }
  368. Data = 0;
  369. pData = (UINT8 *)&Data;
  370. down(&I2cDev.I2cMutex[Mx]);
  371. MxI2CStart(Mx,lag);
  372. MxI2CWriteByte(Mx,SlaveAddress,lag);
  373. MxI2CWriteByte(Mx,BaseAddress,lag);
  374. MxI2CRestart(Mx,lag);
  375. MxI2CWriteByte(Mx,SlaveAddress|1,lag);
  376. for(i=0;i<Length-1;i++)
  377. {
  378. pData[i] = MxI2CReadByte(Mx,lag,TRUE);
  379. }
  380. pData[i] = MxI2CReadByte(Mx,lag,FALSE);
  381. MxI2CStop(Mx,lag);
  382. *pStatus = I2cDev.Status[Mx];
  383. if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
  384. {
  385. DbgFunPrint("Read M%d %02x %02x Fail %d",Mx,SlaveAddress,BaseAddress,Length);
  386. }
  387. up(&I2cDev.I2cMutex[Mx]);
  388. return Data;
  389. }
  390. void SwI2CWriteBasicFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 BaseAddress,UINT32 Data,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
  391. {
  392. UINT32 lag,i;
  393. UINT8 *pData;
  394. if(Length>4)
  395. {
  396. *pStatus = I2C_STATUS_DATA_ERROR;
  397. DbgFunPrint("Invalid Parameter");
  398. return;
  399. }
  400. if(in_atomic())
  401. {
  402. if(QueueI2CFun(Mx,SlaveAddress,ADDRESS_TYPE_BYTE,BaseAddress,(UINT8 *)&Data,Length,Speed)<0)
  403. {
  404. DbgFunPrint("Invalid Process!!");
  405. *pStatus = I2C_STATUS_DATA_ERROR;
  406. }
  407. else
  408. {
  409. *pStatus = I2C_STATUS_SUCCESS;
  410. }
  411. return;
  412. }
  413. if(Speed==I2C_SPEED_100K)
  414. {
  415. lag = SW_MASTER_100K;
  416. }
  417. else
  418. {
  419. lag = SW_MASTER_400K;
  420. }
  421. pData = (UINT8 *)&Data;
  422. down(&I2cDev.I2cMutex[Mx]);
  423. MxI2CStart(Mx,lag);
  424. MxI2CWriteByte(Mx,SlaveAddress,lag);
  425. MxI2CWriteByte(Mx,BaseAddress,lag);
  426. for(i=0;i<Length;i++)
  427. {
  428. MxI2CWriteByte(Mx,pData[i],lag);
  429. }
  430. MxI2CStop(Mx,lag);
  431. *pStatus = I2cDev.Status[Mx];
  432. if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
  433. {
  434. DbgFunPrint("Write M%d %02x %02x Fail %d 0x%08x",Mx,SlaveAddress,BaseAddress,Length,Data);
  435. }
  436. up(&I2cDev.I2cMutex[Mx]);
  437. }
  438. #endif
  439. void DRV_SwI2C_ReadFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 AddrType,UINT32 BaseAddress,UINT8 *pBuffer,UINT16 Length,UINT8 Speed,UINT8 *pStatus)
  440. {
  441. UINT16 i;
  442. UINT32 lag;
  443. if(in_atomic())
  444. {
  445. DbgFunPrint("ID=%02x,Invalid Process!!",SlaveAddress);
  446. *pStatus = I2C_STATUS_DATA_ERROR;
  447. return;
  448. }
  449. if(Speed==I2C_SPEED_100K)
  450. {
  451. lag = SW_MASTER_100K;
  452. }
  453. else
  454. {
  455. lag = SW_MASTER_400K;
  456. }
  457. down(&I2cDev.I2cMutex[Mx]);
  458. MxI2CStart(Mx,lag);
  459. if(AddrType==ADDRESS_TYPE_BYTE)
  460. {
  461. MxI2CWriteByte(Mx,SlaveAddress,lag);
  462. MxI2CWriteByte(Mx,(UINT8)BaseAddress,lag);
  463. MxI2CRestart(Mx,lag);
  464. MxI2CWriteByte(Mx,SlaveAddress|1,lag);
  465. }
  466. else if(AddrType==ADDRESS_TYPE_NONE)
  467. {
  468. MxI2CWriteByte(Mx,SlaveAddress|1,lag);
  469. }
  470. else if(AddrType==ADDRESS_TYPE_MULTIPLE)
  471. {
  472. UINT8 AddrNum;
  473. AddrNum = (BaseAddress>>24) & 0xFF;
  474. if(AddrNum>3 || AddrNum==0)
  475. {
  476. MxI2CStop(Mx,lag);
  477. *pStatus = I2C_STATUS_DATA_ERROR;
  478. up(&I2cDev.I2cMutex[Mx]);
  479. DbgFunPrint("Invalid AddrNum");
  480. return;
  481. }
  482. MxI2CWriteByte(Mx,SlaveAddress,lag);
  483. for(i=0;i<AddrNum;i++)
  484. {
  485. MxI2CWriteByte(Mx,(UINT8)(BaseAddress>>(8*i)),lag);
  486. }
  487. MxI2CRestart(Mx,lag);
  488. MxI2CWriteByte(Mx,SlaveAddress|1,lag);
  489. }
  490. for(i=0;i<Length-1;i++)
  491. {
  492. pBuffer[i] = MxI2CReadByte(Mx,lag,TRUE);
  493. }
  494. pBuffer[i] = MxI2CReadByte(Mx,lag,FALSE);
  495. MxI2CStop(Mx,lag);
  496. *pStatus = I2cDev.Status[Mx];
  497. if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
  498. {
  499. DbgFunPrint("Multi-Read M%d %02x Fail",Mx,SlaveAddress);
  500. }
  501. up(&I2cDev.I2cMutex[Mx]);
  502. }
  503. #if 0
  504. void SwI2CReadMultiBytesTempFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 AddrType,UINT32 BaseAddress,UINT8 AddrLength,UINT8 *pBuffer,UINT16 DataLength,UINT8 Speed,UINT8 *pStatus)
  505. {
  506. UINT16 i;
  507. UINT32 lag;
  508. DebugPrint("SwI2CReadMultiBytesTempFun DeviceId = 0x%02x AddrType = %d BaseAddress = 0x%08x AddrLength = %d DataLength = %d Speed = %d",SlaveAddress,AddrType,BaseAddress,AddrLength,DataLength,Speed);
  509. if(in_atomic())
  510. {
  511. DbgFunPrint("ID=%02x,Invalid Process!!",SlaveAddress);
  512. *pStatus = I2C_STATUS_DATA_ERROR;
  513. return;
  514. }
  515. if(Speed==I2C_SPEED_100K)
  516. {
  517. lag = SW_MASTER_100K;
  518. }
  519. else
  520. {
  521. lag = SW_MASTER_400K;
  522. }
  523. down(&I2cDev.I2cMutex[Mx]);
  524. MxI2CStart(Mx,lag);
  525. if(AddrType==ADDRESS_TYPE_BYTE)
  526. {
  527. MxI2CWriteByte(Mx,SlaveAddress,lag);
  528. MxI2CWriteByte(Mx,(UINT8)BaseAddress,lag);
  529. MxI2CRestart(Mx,lag);
  530. MxI2CWriteByte(Mx,SlaveAddress|1,lag);
  531. }
  532. else if(AddrType==ADDRESS_TYPE_NONE)
  533. {
  534. MxI2CWriteByte(Mx,SlaveAddress|1,lag);
  535. }
  536. else if(AddrType==ADDRESS_TYPE_MULTIPLE)
  537. {
  538. if(AddrLength>4 || AddrLength==0)
  539. {
  540. MxI2CStop(Mx,lag);
  541. *pStatus = I2C_STATUS_DATA_ERROR;
  542. up(&I2cDev.I2cMutex[Mx]);
  543. DbgFunPrint("Invalid AddrNum");
  544. return;
  545. }
  546. MxI2CWriteByte(Mx,SlaveAddress,lag);
  547. if (AddrLength==4)
  548. {
  549. for(i=0;i<AddrLength;i++)
  550. {
  551. MxI2CWriteByte(Mx,(UINT8)(BaseAddress>>(8*i)),lag);
  552. }
  553. }
  554. else if (AddrLength==2)
  555. {
  556. UINT16 Addr = (UINT16)BaseAddress;
  557. for(i=0;i<AddrLength;i++)
  558. {
  559. MxI2CWriteByte(Mx,(UINT8)(Addr>>(8*i)),lag);
  560. }
  561. }
  562. else if (AddrLength==1)
  563. {
  564. MxI2CWriteByte(Mx,(UINT8)BaseAddress,lag);
  565. }
  566. MxI2CRestart(Mx,lag);
  567. MxI2CWriteByte(Mx,SlaveAddress|1,lag);
  568. }
  569. for(i=0;i<DataLength-1;i++)
  570. {
  571. pBuffer[i] = MxI2CReadByte(Mx,lag,TRUE);
  572. }
  573. pBuffer[i] = MxI2CReadByte(Mx,lag,FALSE);
  574. MxI2CStop(Mx,lag);
  575. *pStatus = I2cDev.Status[Mx];
  576. if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
  577. {
  578. DbgFunPrint("Multi-Read M%d %02x Fail",Mx,SlaveAddress);
  579. }
  580. up(&I2cDev.I2cMutex[Mx]);
  581. }
  582. void SwI2CWriteMultiBytesFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 AddrType,UINT32 BaseAddress,UINT8 *pBuffer,UINT16 Length,UINT8 Speed,UINT8 *pStatus)
  583. {
  584. UINT32 i;
  585. UINT32 lag;
  586. if(in_atomic())
  587. {
  588. if(QueueI2CFun(Mx,SlaveAddress,AddrType,BaseAddress,pBuffer,Length,Speed)<0)
  589. {
  590. DbgFunPrint("Invalid Process!!");
  591. *pStatus = I2C_STATUS_DATA_ERROR;
  592. }
  593. else
  594. {
  595. *pStatus = I2C_STATUS_SUCCESS;
  596. }
  597. return;
  598. }
  599. if(Speed==I2C_SPEED_100K)
  600. {
  601. lag = SW_MASTER_100K;
  602. }
  603. else
  604. {
  605. lag = SW_MASTER_400K;
  606. }
  607. down(&I2cDev.I2cMutex[Mx]);
  608. MxI2CStart(Mx,lag);
  609. MxI2CWriteByte(Mx,SlaveAddress,lag);
  610. if(AddrType==ADDRESS_TYPE_BYTE)
  611. {
  612. MxI2CWriteByte(Mx,(UINT8)BaseAddress,lag);
  613. }
  614. else if(AddrType==ADDRESS_TYPE_MULTIPLE)
  615. {
  616. UINT8 AddrNum;
  617. AddrNum = (BaseAddress>>24) & 0xFF;
  618. if(AddrNum>3 || AddrNum==0)
  619. {
  620. MxI2CStop(Mx,lag);
  621. *pStatus = I2C_STATUS_DATA_ERROR;
  622. up(&I2cDev.I2cMutex[Mx]);
  623. DbgFunPrint("Invalid AddrNum");
  624. return;
  625. }
  626. for(i=0;i<AddrNum;i++)
  627. {
  628. MxI2CWriteByte(Mx,(UINT8)(BaseAddress>>(8*i)),lag);
  629. }
  630. }
  631. for(i=0;i<Length;i++)
  632. {
  633. MxI2CWriteByte(Mx,pBuffer[i],lag);
  634. }
  635. MxI2CStop(Mx,lag);
  636. *pStatus = I2cDev.Status[Mx];
  637. if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
  638. {
  639. DbgFunPrint("Multi-Write M%d %02x Fail",Mx,SlaveAddress);
  640. }
  641. up(&I2cDev.I2cMutex[Mx]);
  642. }
  643. #endif
  644. #endif
  645. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
  646. #if INTERRUPT_ENABLE
  647. static irqreturn_t MasterAI2cInterruptFun(INT32 irq,void *dev_id)
  648. {
  649. UINT32 BusStatus,Control0,Remainder,Data;
  650. UINT8 IntReg,CopyBytes;
  651. UINT8* pBuffer;
  652. // DebugPrint("MasterI2cInterruptFun");
  653. BusStatus = MmioReadFun(M0_BUS_STATUS);
  654. // Master 0
  655. Control0 = MmioReadFun(M0_CONTROL0);
  656. if(Control0&MX_CONTROL0_INT)
  657. {
  658. IntReg = (UINT8)(BusStatus & 0xFF);
  659. // DebugPrint("BusStatus=0x%08x",BusStatus);
  660. if(IntReg==0x00||IntReg==0x0B||IntReg==0xFF)
  661. {
  662. I2cDev.Status[0] = I2C_STATUS_DEVICE_ID_ERROR;
  663. complete(&I2cDev.Comp[0]);
  664. //del_timer(&I2cDev.ResetTimer[0]);
  665. DebugPrint("M0 %08x I2C Error!!! 0x%02x",MmioReadFun(M0_CONTROL1),IntReg);
  666. }
  667. else
  668. {
  669. I2cDev.InterruptCounter[0]--;
  670. pBuffer = I2cDev.IntInfo[0].pBuffer;
  671. Remainder = I2cDev.IntInfo[0].Length - I2cDev.IntInfo[0].Completed;
  672. #if ONE_BYTE_INTERRUPT
  673. if( (I2cDev.InterruptCounter[0]==0||IntReg==0x04||IntReg==0x08) && pBuffer!=NULL && Remainder>0 )
  674. #else
  675. if( (IntReg==0x09||IntReg==0x0A) && pBuffer!=NULL && Remainder>0 )
  676. #endif
  677. {
  678. if(Remainder>4)
  679. {
  680. CopyBytes = 4;
  681. }
  682. else
  683. {
  684. CopyBytes = Remainder;
  685. }
  686. if(I2cDev.IntInfo[0].bRead)
  687. {
  688. #if ONE_BYTE_INTERRUPT
  689. if(IntReg==0x05||IntReg==0x06||IntReg==0x07||IntReg==0x08)
  690. #else
  691. if(IntReg==0x0A)
  692. #endif
  693. {
  694. Data = MmioReadFun(M0_DATA1);
  695. }
  696. else
  697. {
  698. Data = MmioReadFun(M0_DATA0);
  699. }
  700. memcpy(&pBuffer[I2cDev.IntInfo[0].Completed],&Data,CopyBytes);
  701. }
  702. else
  703. {
  704. memcpy(&Data,&pBuffer[I2cDev.IntInfo[0].Completed],CopyBytes);
  705. #if ONE_BYTE_INTERRUPT
  706. if(IntReg==0x08)
  707. #else
  708. if(IntReg==0x0A)
  709. #endif
  710. {
  711. MmioWriteFun(M0_DATA1,Data);
  712. }
  713. else
  714. {
  715. MmioWriteFun(M0_DATA0,Data);
  716. }
  717. }
  718. I2cDev.IntInfo[0].Completed += CopyBytes;
  719. }
  720. if(I2cDev.InterruptCounter[0]==0)
  721. {
  722. complete(&I2cDev.Comp[0]);
  723. //del_timer(&I2cDev.ResetTimer[0]);
  724. }
  725. }
  726. MmioWriteFun(M0_CONTROL0,(Control0&0xF0FFFFFF)|MX_CONTROL0_INT_CLEAR);
  727. }
  728. return IRQ_HANDLED;
  729. }
  730. static irqreturn_t MasterBI2cInterruptFun(INT32 irq,void *dev_id)
  731. {
  732. UINT32 BusStatus,Control0,Remainder,Data;
  733. UINT8 IntReg,CopyBytes;
  734. UINT8* pBuffer;
  735. BusStatus = MmioReadFun(M1_BUS_STATUS);
  736. // Master 1
  737. Control0 = MmioReadFun(M1_CONTROL0);
  738. if(Control0&MX_CONTROL0_INT)
  739. {
  740. IntReg = (UINT8)(BusStatus& 0xFF);
  741. // DebugPrint("BusStatus=0x%08x",BusStatus);
  742. if(IntReg==0x00||IntReg==0x0B||IntReg==0xFF)
  743. {
  744. I2cDev.Status[1] = I2C_STATUS_DEVICE_ID_ERROR;
  745. complete(&I2cDev.Comp[1]);
  746. DebugPrint("M1 %08x I2C Error!!! 0x%02x",MmioReadFun(M1_CONTROL1),IntReg);
  747. }
  748. else
  749. {
  750. I2cDev.InterruptCounter[1]--;
  751. pBuffer = I2cDev.IntInfo[1].pBuffer;
  752. Remainder = I2cDev.IntInfo[1].Length - I2cDev.IntInfo[1].Completed;
  753. #if ONE_BYTE_INTERRUPT
  754. if( (I2cDev.InterruptCounter[0]==0||IntReg==0x04||IntReg==0x08) && pBuffer!=NULL && Remainder>0 )
  755. #else
  756. if( (IntReg==0x09||IntReg==0x0A) && pBuffer!=NULL && Remainder>0 )
  757. #endif
  758. {
  759. if(Remainder>4)
  760. {
  761. CopyBytes = 4;
  762. }
  763. else
  764. {
  765. CopyBytes = Remainder;
  766. }
  767. if(I2cDev.IntInfo[1].bRead)
  768. {
  769. #if ONE_BYTE_INTERRUPT
  770. if(IntReg==0x05||IntReg==0x06||IntReg==0x07||IntReg==0x08)
  771. #else
  772. if(IntReg==0x0A)
  773. #endif
  774. {
  775. Data = MmioReadFun(M1_DATA1);
  776. }
  777. else
  778. {
  779. Data = MmioReadFun(M1_DATA0);
  780. }
  781. memcpy(&pBuffer[I2cDev.IntInfo[1].Completed],&Data,CopyBytes);
  782. }
  783. else
  784. {
  785. memcpy(&Data,&pBuffer[I2cDev.IntInfo[1].Completed],CopyBytes);
  786. #if ONE_BYTE_INTERRUPT
  787. if(IntReg==0x08)
  788. #else
  789. if(IntReg==0x0A)
  790. #endif
  791. {
  792. MmioWriteFun(M1_DATA1,Data);
  793. }
  794. else
  795. {
  796. MmioWriteFun(M1_DATA0,Data);
  797. }
  798. }
  799. I2cDev.IntInfo[1].Completed += CopyBytes;
  800. }
  801. if(I2cDev.InterruptCounter[1]==0)
  802. {
  803. complete(&I2cDev.Comp[1]);
  804. }
  805. }
  806. MmioWriteFun(M1_CONTROL0,(Control0&0xF0FFFFFF)|MX_CONTROL0_INT_CLEAR);
  807. }
  808. return IRQ_HANDLED;
  809. }
  810. //static void i2c_dispatch(struct pt_regs *regs)
  811. static void i2c_dispatch_A(void)
  812. {
  813. do_IRQ(MASTER_A_IRQ_FINAL);
  814. }
  815. static void i2c_dispatch_B(void)
  816. {
  817. do_IRQ(MASTER_B_IRQ_FINAL);
  818. }
  819. /*
  820. static void i2cM0TimeOutFun(UINT32 data)
  821. {
  822. UINT32 reg;
  823. #ifndef INIT_BY_KMF
  824. UINT32 i;
  825. #endif
  826. DbgFunPrint("Critical Error occurs, InterruptCounter=%d",I2cDev.InterruptCounter[0]);
  827. #ifndef INIT_BY_KMF
  828. for(i=0;i<0x100;)
  829. {
  830. if((i&0xF)==0)
  831. printk("0x%08X: ",(UINT32)(M0_CONTROL0+i));
  832. printk("%08X ",MmioReadFun(M0_CONTROL0+i));
  833. i += 4;
  834. if((i&0xF)==0)
  835. printk("\n");
  836. }
  837. if((i&0xF)!=0)
  838. printk("\n");
  839. printk("\n");
  840. #endif
  841. reg = MmioReadFun(M0_CONTROL0);
  842. reg &= 0x00FF0000;
  843. reg |= 0x000000FF;
  844. MmioWriteFun(M0_CONTROL0,reg);
  845. I2cDev.Status[0] = I2C_STATUS_HARDWARE_CRASH;
  846. complete(&I2cDev.Comp[0]);
  847. }
  848. static void i2cM1TimeOutFun(UINT32 data)
  849. {
  850. UINT32 reg;
  851. #ifndef INIT_BY_KMF
  852. UINT32 i;
  853. #endif
  854. DebugPrint("i2cM1TimeOutFun, InterruptCounter=%d",I2cDev.InterruptCounter[1]);
  855. #ifndef INIT_BY_KMF
  856. for(i=0;i<0x100;)
  857. {
  858. if((i&0xF)==0)
  859. printk("0x%08X: ",(UINT32)(M0_CONTROL0+i));
  860. printk("%08X ",MmioReadFun(M0_CONTROL0+i));
  861. i += 4;
  862. if((i&0xF)==0)
  863. printk("\n");
  864. }
  865. if((i&0xF)!=0)
  866. printk("\n");
  867. printk("\n");
  868. #endif
  869. reg = MmioReadFun(M1_CONTROL0);
  870. reg &= 0x00FF0000;
  871. reg |= 0x000000FF;
  872. MmioWriteFun(M1_CONTROL0,reg);
  873. I2cDev.Status[1] = I2C_STATUS_HARDWARE_CRASH;
  874. complete(&I2cDev.Comp[1]);
  875. }*/
  876. #else // else of #if INTERRUPT_ENABLE
  877. void MasterPollingFun(UINT8 MasterIndex,UINT16 Times)
  878. {
  879. INT32 Count;
  880. UINT32 BusStatus,Control0;
  881. UINT8 IntReg;
  882. PVOID Address;
  883. if(MasterIndex==0)
  884. {
  885. Address = (PVOID)M0_CONTROL0;
  886. }
  887. else if(MasterIndex==1)
  888. {
  889. Address = (PVOID)M1_CONTROL0;
  890. }
  891. else
  892. {
  893. DbgFunPrint("Invalid Master Index");
  894. return;
  895. }
  896. Count = 0;
  897. Control0 = MmioReadFun(Address);
  898. while(Count<Times && I2cDev.InterruptCounter[MasterIndex]>0)
  899. {
  900. BusStatus = MmioReadFun(M0_BUS_STATUS);
  901. IntReg = (UINT8)(BusStatus >> (16*MasterIndex));
  902. if(IntReg==0x00||IntReg==0x0B||IntReg==0x10||IntReg==0x1B||IntReg==0xFF)
  903. {
  904. MmioWriteFun(Address,(Control0&0x00FFFFFF)|MX_CONTROL0_INT_CLEAR);
  905. DebugPrint("IntReg==0x%02x",IntReg);
  906. I2cDev.Status[MasterIndex] = I2C_STATUS_DEVICE_ID_ERROR;
  907. break;
  908. }
  909. else
  910. {
  911. MmioWriteFun(Address,(Control0&0x00FFFFFF)|MX_CONTROL0_INT_CLEAR);
  912. Count++;
  913. I2cDev.InterruptCounter[MasterIndex]--;
  914. }
  915. }
  916. }
  917. #endif // end of #if INTERRUPT_ENABLE
  918. static void MasterAsyncResetFun(UINT8 MasterIndex,UINT8 Speed)
  919. {
  920. UINT32 RegValue,Scl;
  921. if(Speed==I2C_SPEED_25K)
  922. {
  923. Scl = MX_CONTROL0_SCL_25K;
  924. }
  925. else if(Speed==I2C_SPEED_100K)
  926. {
  927. Scl = MX_CONTROL0_SCL_100K;
  928. }
  929. else if(Speed==I2C_SPEED_200K)
  930. {
  931. Scl = MX_CONTROL0_SCL_200K;
  932. }
  933. else if(Speed==I2C_SPEED_300K)
  934. {
  935. Scl = MX_CONTROL0_SCL_300K;
  936. }
  937. else if(Speed==I2C_SPEED_400K)
  938. {
  939. Scl = MX_CONTROL0_SCL_400K;
  940. }
  941. else
  942. {
  943. Scl = MX_CONTROL0_SCL_100K;
  944. }
  945. if(MasterIndex)
  946. {
  947. RegValue = MmioReadFun(M1_CONTROL0);
  948. if((RegValue&0xFFFF)!=Scl)
  949. {
  950. // DebugPrint("MasterAsyncResetFun");
  951. RegValue &= 0x00FF0000;
  952. RegValue |= MX_CONTROL0_ASYNC_RESET|Scl;
  953. wmb();
  954. MmioWriteFun(M1_CONTROL0,RegValue|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
  955. udelay(1);
  956. RegValue &= ~MX_CONTROL0_ASYNC_RESET;
  957. wmb();
  958. MmioWriteFun(M1_CONTROL0,RegValue|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
  959. udelay(1);
  960. }
  961. }
  962. else
  963. {
  964. RegValue = MmioReadFun(M0_CONTROL0);
  965. if((RegValue&0xFFFF)!=Scl)
  966. {
  967. // DebugPrint("MasterAsyncResetFun");
  968. RegValue &= 0x00FF0000;
  969. RegValue |= MX_CONTROL0_ASYNC_RESET|Scl;
  970. wmb();
  971. MmioWriteFun(M0_CONTROL0,RegValue|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
  972. udelay(1);
  973. RegValue &= ~MX_CONTROL0_ASYNC_RESET;
  974. wmb();
  975. MmioWriteFun(M0_CONTROL0,RegValue|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
  976. udelay(1);
  977. }
  978. }
  979. }
  980. void SW_I2C_DummyRead(UINT8 MasterIndex, UINT8 speed)
  981. {
  982. PVOID MxSWI2CBUS;
  983. UINT8 delayTime = 0, index=0;
  984. printk("<0>""[I2C][%s]!!\n",__FUNCTION__);
  985. if ( MasterIndex == 0) {
  986. MxSWI2CBUS = (PVOID)(M0_CONTROL0+0x100);
  987. }
  988. else {
  989. MxSWI2CBUS = (PVOID)(M1_CONTROL0+0x100);
  990. }
  991. switch (speed) {
  992. case I2C_SPEED_25K:
  993. delayTime = 20;
  994. break;
  995. case I2C_SPEED_100K:
  996. delayTime = 5;
  997. break;
  998. case I2C_SPEED_200K:
  999. delayTime = 3;
  1000. break;
  1001. case I2C_SPEED_300K:
  1002. case I2C_SPEED_400K:
  1003. delayTime = 2;
  1004. break;
  1005. default:
  1006. delayTime = 5; // 100K
  1007. break;
  1008. }
  1009. MmioWriteFun(MxSWI2CBUS, 0x0c);
  1010. for ( index=0;index<9;index++ ) {
  1011. MmioWriteFun(MxSWI2CBUS, 0x4c);
  1012. udelay(delayTime);
  1013. MmioWriteFun(MxSWI2CBUS, 0x48);
  1014. udelay(delayTime);
  1015. }
  1016. MmioWriteFun(MxSWI2CBUS, 0x40);
  1017. udelay(delayTime);
  1018. MmioWriteFun(MxSWI2CBUS, 0x44);
  1019. udelay(delayTime);
  1020. MmioWriteFun(MxSWI2CBUS, 0x4c);
  1021. udelay(delayTime);
  1022. MmioWriteFun(MxSWI2CBUS, 0x0c);
  1023. }
  1024. void I2CRead(UINT8 MasterIndex,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed,UINT8 *pStatus)
  1025. {
  1026. UINT32 RegValue,ucStatus;
  1027. PVOID MxControl0,MxControl1,__attribute__((unused)) MxData[2],MxAddress,MxDataLength;
  1028. #if !INTERRUPT_ENABLE
  1029. UINT32 Remainder,Completed;
  1030. UINT8 CopyBytes,Switch=0;
  1031. #endif
  1032. UINT8 I2CRead_RETRY_CNT=0;
  1033. // DebugPrint("DRV_I2C_ReadFun 0x%02x 0x%02x %d",DeviceID,Address,Length);
  1034. /*
  1035. if(MasterIndex>=MASTER_NUMBER || AddrType>=ADDRESS_TYPE_INVALID ||
  1036. (AddrType==ADDRESS_TYPE_MULTIPLE&&(Address&0x03000000)==0) ||
  1037. Length == 0 || pData == NULL)
  1038. {
  1039. DbgFunPrint("Invalid Parameter");
  1040. *pStatus = I2C_STATUS_DATA_ERROR;
  1041. return;
  1042. }
  1043. if(in_atomic())
  1044. {
  1045. DbgFunPrint("ID=%02x,Invalid Process!!",DeviceID);
  1046. *pStatus = I2C_STATUS_DATA_ERROR;
  1047. return;
  1048. }
  1049. down(&I2cDev.I2cMutex[MasterIndex]);
  1050. */
  1051. /* if(down_interruptible(&I2cDev.I2cMutex[MasterIndex]))
  1052. {
  1053. DbgFunPrint("SYSTEM_ERROR");
  1054. *pStatus = I2C_STATUS_SYSTEM_ERROR;
  1055. return;
  1056. }*/
  1057. #if 0
  1058. if (bSuspend)
  1059. {
  1060. DbgFunPrint("System is suspend!!!!!!!");
  1061. // up(&I2cDev.I2cMutex[MasterIndex]);
  1062. *pStatus = I2C_STATUS_SYSTEM_ERROR;
  1063. return;
  1064. }
  1065. #endif
  1066. I2CRead_retry:
  1067. if(g_ucHangNum>1)
  1068. g_BusyLoop = 2;
  1069. else
  1070. g_BusyLoop = 100;
  1071. RegValue = 0;
  1072. // BusyCounter = 0;
  1073. if(MasterIndex==0)
  1074. {
  1075. MxControl0 = (PVOID)M0_CONTROL0;
  1076. MxControl1 = (PVOID)M0_CONTROL1;
  1077. MxData[0] = (PVOID)M0_DATA0;
  1078. MxData[1] = (PVOID)M0_DATA1;
  1079. MxAddress = (PVOID)M0_BASE_ADDRESS;
  1080. MxDataLength = (PVOID)M0_DATA_LENGTH;
  1081. M0BusyCounter = 0;
  1082. while((MmioReadFun(M0_BUS_STATUS) & I2C_BUS_STATUS_BUSY)==I2C_BUS_STATUS_BUSY)
  1083. {
  1084. //DbgFunPrint("I2C0BusBusy 0x%08x %d",MmioReadFun(M0_BUS_STATUS),RegValue++);
  1085. M0BusyCounter++;
  1086. DbgFunPrint("I2C0BusBusy 0x%08x,BusyCount=%d",MmioReadFun(M0_BUS_STATUS),M0BusyCounter);
  1087. msleep(10);
  1088. if (M0BusyCounter > g_BusyLoop)
  1089. {
  1090. // printk(KERN_EMERG "[I2C WARNING]I2C0BusBusy!!!!!!!!!!!!\n");
  1091. *pStatus = I2C_STATUS_DATA_ERROR;
  1092. // up(&I2cDev.I2cMutex[MasterIndex]);
  1093. // if(1000 == g_BusyLoop)
  1094. {
  1095. g_ucHangNum++;
  1096. if(0==(g_ucHangNum%10))
  1097. {
  1098. printk("<0>""[I2C][%s]0_I2C BUS BUSY!!,g_ucHangNum=%d\n",__FUNCTION__,g_ucHangNum);
  1099. printk("<0>""MasterIndex = %d",MasterIndex);
  1100. printk("<0>""DeviceID = 0x%x",DeviceID);
  1101. printk("<0>""AddrType = %d",AddrType);
  1102. printk("<0>""Address = %d",Address);
  1103. printk("<0>""Length = %d",Length);
  1104. printk("<0>""Speed = %d",Speed);
  1105. }
  1106. }
  1107. return;
  1108. //break;
  1109. }
  1110. }
  1111. g_ucHangNum = 0;
  1112. }
  1113. else// if(MasterIndex==1)
  1114. {
  1115. MxControl0 = (PVOID)M1_CONTROL0;
  1116. MxControl1 = (PVOID)M1_CONTROL1;
  1117. MxData[0] = (PVOID)M1_DATA0;
  1118. MxData[1] = (PVOID)M1_DATA1;
  1119. MxAddress = (PVOID)M1_BASS_ADDRESS;
  1120. MxDataLength = (PVOID)M1_DATA_LENGTH;
  1121. M1BusyCounter = 0;
  1122. while((MmioReadFun(M1_BUS_STATUS) & I2C_BUS_STATUS_BUSY)==I2C_BUS_STATUS_BUSY)
  1123. {
  1124. //DbgFunPrint("I2C1BusBusy %d",RegValue++);
  1125. M1BusyCounter++;
  1126. DbgFunPrint("I2C1BusBusy,BusyCount=%d",M1BusyCounter);
  1127. #if 0//UMF_OPT_INTERNETMONITOR_SPEC
  1128. #else
  1129. msleep(10);
  1130. #endif
  1131. if (M1BusyCounter > g_BusyLoop)
  1132. {
  1133. // printk(KERN_EMERG "[I2C WARNING]I2C1BusBusy!!!!!!!!!!!!\n");
  1134. *pStatus = I2C_STATUS_DATA_ERROR;
  1135. // up(&I2cDev.I2cMutex[MasterIndex]);
  1136. // if(1000 == g_BusyLoop)
  1137. {
  1138. g_ucHangNum++;
  1139. if(0==(g_ucHangNum%10))
  1140. {
  1141. printk("<0>""[I2C][%s]0_I2C BUS BUSY!!,g_ucHangNum=%d\n",__FUNCTION__,g_ucHangNum);
  1142. printk("<0>""MasterIndex = %d",MasterIndex);
  1143. printk("<0>""DeviceID = 0x%x",DeviceID);
  1144. printk("<0>""AddrType = %d",AddrType);
  1145. printk("<0>""Address = %d",Address);
  1146. printk("<0>""Length = %d",Length);
  1147. printk("<0>""Speed = %d",Speed);
  1148. }
  1149. }
  1150. return;
  1151. //break;
  1152. }
  1153. }
  1154. g_ucHangNum = 0;
  1155. }
  1156. MasterAsyncResetFun(MasterIndex,Speed); // Asynchronous reset
  1157. // to avoid warning message
  1158. // MxData[0] = MxData[0];
  1159. // MxData[1] = MxData[1];
  1160. // To Do
  1161. DeviceID &= 0xFE;
  1162. if(AddrType==ADDRESS_TYPE_BYTE)
  1163. {
  1164. RegValue = ((UINT32)Length)<<16;
  1165. MmioWriteFun(MxDataLength,RegValue); // Data Length
  1166. RegValue = 0x01000000 | (Address & 0xFF);
  1167. MmioWriteFun(MxAddress,RegValue); // Address
  1168. RegValue = ((UINT32)DeviceID) | (((UINT32)DeviceID | 0x01)<<16);
  1169. MmioWriteFun(MxControl1,RegValue);
  1170. RegValue = 0x03800000;
  1171. //I2cDev.InterruptCounter[MasterIndex] = 1;
  1172. }
  1173. /* else if(AddrType==ADDRESS_TYPE_WORD)
  1174. {
  1175. }*/
  1176. else if(AddrType==ADDRESS_TYPE_NONE)
  1177. {
  1178. MmioWriteFun(MxDataLength,(UINT32)Length); // Data Length
  1179. RegValue = ((UINT32)DeviceID | 0x01);
  1180. MmioWriteFun(MxControl1,RegValue);
  1181. RegValue = 0x02800000;
  1182. // RegValue = 0x02800000 | MX_CONTROL0_NO_ADDRESS;
  1183. //I2cDev.InterruptCounter[MasterIndex] = 0;
  1184. }
  1185. else if(AddrType==ADDRESS_TYPE_MULTIPLE)
  1186. {
  1187. RegValue = ((UINT32)Length)<<16;
  1188. MmioWriteFun(MxDataLength,RegValue); // Data Length
  1189. MmioWriteFun(MxAddress,Address&0x03FFFFFF); // Address
  1190. RegValue = ((UINT32)DeviceID) | (((UINT32)DeviceID | 0x01)<<16);
  1191. MmioWriteFun(MxControl1,RegValue);
  1192. RegValue = 0x03800000;
  1193. //I2cDev.InterruptCounter[MasterIndex] = 1;
  1194. }
  1195. I2cDev.InterruptCounter[MasterIndex] = 0;
  1196. if(Speed==I2C_SPEED_25K)
  1197. {
  1198. RegValue |= MX_CONTROL0_SCL_25K;
  1199. }
  1200. else if(Speed==I2C_SPEED_100K)
  1201. {
  1202. RegValue |= MX_CONTROL0_SCL_100K;
  1203. }
  1204. else if(Speed==I2C_SPEED_200K)
  1205. {
  1206. RegValue |= MX_CONTROL0_SCL_200K;
  1207. }
  1208. else if(Speed==I2C_SPEED_300K)
  1209. {
  1210. RegValue |= MX_CONTROL0_SCL_300K;
  1211. }
  1212. else if(Speed==I2C_SPEED_400K)
  1213. {
  1214. RegValue |= MX_CONTROL0_SCL_400K;
  1215. }
  1216. I2cDev.Status[MasterIndex] = I2C_STATUS_SUCCESS;
  1217. #if INTERRUPT_ENABLE
  1218. I2cDev.IntInfo[MasterIndex].pBuffer = pData;
  1219. I2cDev.IntInfo[MasterIndex].Length = Length;
  1220. I2cDev.IntInfo[MasterIndex].Completed = 0;
  1221. I2cDev.IntInfo[MasterIndex].bRead = TRUE;
  1222. #if ONE_BYTE_INTERRUPT
  1223. I2cDev.InterruptCounter[MasterIndex] += Length;
  1224. RegValue |= MX_CONTROL0_INT_EN|MX_CONTROL0_ONE_BYTE_INT|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE;
  1225. #else
  1226. I2cDev.InterruptCounter[MasterIndex] += (Length+3)/4;
  1227. RegValue |= MX_CONTROL0_INT_EN|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE;
  1228. #endif
  1229. //if(MasterIndex==0)
  1230. // mod_timer(&I2cDev.ResetTimer[0],jiffies+(Length+49)/50*(HZ/25));
  1231. wmb();
  1232. MmioWriteFun(MxControl0,RegValue);
  1233. //20120720 DylanWang, use timeout to avoid watch dog reboot the system
  1234. //wait 2s
  1235. //wait_for_completion(&I2cDev.Comp[MasterIndex]);
  1236. ucStatus = wait_for_completion_timeout(&I2cDev.Comp[MasterIndex], 2*HZ);
  1237. if(ucStatus<2)
  1238. {
  1239. g_ucHangNum++;
  1240. printk("<0>""[I2C][%s]wait completion timeout!!\n",__FUNCTION__);
  1241. *pStatus = I2C_STATUS_SYSTEM_TIMEOUT;
  1242. #if 1
  1243. SW_I2C_DummyRead(MasterIndex, Speed);
  1244. udelay(100*1000);//wait 100 ms to clear remain interrupts
  1245. init_completion(&I2cDev.Comp[MasterIndex]); //reset complete count
  1246. if(I2CRead_RETRY_CNT == 0)
  1247. {
  1248. I2CRead_RETRY_CNT += 1;
  1249. goto I2CRead_retry;
  1250. }
  1251. #else
  1252. return;
  1253. #endif
  1254. }
  1255. #if 0
  1256. if (bSuspend)
  1257. {
  1258. DbgFunPrint("System is suspend!!!!!!!");
  1259. // up(&I2cDev.I2cMutex[MasterIndex]);
  1260. *pStatus = I2C_STATUS_SYSTEM_ERROR;
  1261. return;
  1262. }
  1263. #endif
  1264. MmioWriteFun(0xbe070054,500); //20110120 lwhite add 20us delay between two commands (stop and next start)
  1265. MmioWriteFun(0xbe110054,500);
  1266. /* if(I2cDev.Status[MasterIndex]!=I2C_STATUS_SUCCESS)
  1267. {
  1268. DbgFunPrint("I2C Read %d/%d",I2cDev.IntInfo[MasterIndex].Completed,Length);
  1269. }*/
  1270. #else // else of #if INTERRUPT_ENABLE
  1271. Completed = 0;
  1272. Remainder = Length;
  1273. I2cDev.InterruptCounter[MasterIndex] += Length;
  1274. RegValue |= MX_CONTROL0_ONE_BYTE_INT;
  1275. wmb();
  1276. MmioWriteFun(MxControl0,RegValue);
  1277. if(Length>4)
  1278. {
  1279. CopyBytes = 4;
  1280. }
  1281. else
  1282. {
  1283. CopyBytes = Length;
  1284. }
  1285. MasterPollingFun(MasterIndex,CopyBytes+1);
  1286. RegValue = MmioReadFun(MxData[Switch]);
  1287. Switch ^= 1;
  1288. memcpy(&pData[Completed],&RegValue,CopyBytes);
  1289. Completed += CopyBytes;
  1290. Remainder -= CopyBytes;
  1291. while(Remainder > 0)
  1292. {
  1293. if(Remainder>4)
  1294. {
  1295. CopyBytes = 4;
  1296. }
  1297. else
  1298. {
  1299. CopyBytes = Remainder;
  1300. }
  1301. MasterPollingFun(MasterIndex,CopyBytes);
  1302. RegValue = MmioReadFun(MxData[Switch]);
  1303. Switch ^= 1;
  1304. memcpy(&pData[Completed],&RegValue,CopyBytes);
  1305. Remainder -= CopyBytes;
  1306. Completed += CopyBytes;
  1307. }
  1308. #endif // end of #if INTERRUPT_ENABLE
  1309. *pStatus = I2cDev.Status[MasterIndex];
  1310. }
  1311. void DRV_I2C_ReadFun( UINT8 MasterIndex,
  1312. UINT8 DeviceID,
  1313. UINT8 AddrType,
  1314. UINT32 Address,
  1315. UINT8 *pData,
  1316. UINT16 Length,
  1317. UINT8 Speed,
  1318. UINT8 *pStatus)
  1319. {
  1320. if(MasterIndex>=MASTER_NUMBER || AddrType>=ADDRESS_TYPE_INVALID ||
  1321. (AddrType==ADDRESS_TYPE_MULTIPLE&&(Address&0x03000000)==0) ||
  1322. Length == 0 || pData == NULL)
  1323. {
  1324. DbgFunPrint("Invalid Parameter");
  1325. *pStatus = I2C_STATUS_DATA_ERROR;
  1326. return;
  1327. }
  1328. if(in_atomic())
  1329. {
  1330. DbgFunPrint("ID=%02x,Invalid Process!!",DeviceID);
  1331. *pStatus = I2C_STATUS_DATA_ERROR;
  1332. return;
  1333. }
  1334. down(&I2cDev.I2cMutex[MasterIndex]);
  1335. I2CRead(MasterIndex,DeviceID,AddrType,Address,pData,Length,Speed,pStatus);
  1336. up(&I2cDev.I2cMutex[MasterIndex]);
  1337. }
  1338. void I2CWrite(UINT8 MasterIndex,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed,UINT8 *pStatus)
  1339. {
  1340. UINT32 RegValue,Completed,ucStatus;
  1341. PVOID MxControl0,MxControl1,MxData[2],MxAddress,MxDataLength;
  1342. UINT8 __attribute__((unused)) Remainder,CopyBytes;
  1343. #if !INTERRUPT_ENABLE
  1344. UINT8 Switch=0;
  1345. #endif
  1346. // DebugPrint("DRV_I2C_WriteFun 0x%02x 0x%02x %d",DeviceID,Address,Length);
  1347. /*
  1348. if(MasterIndex>=MASTER_NUMBER || AddrType>=ADDRESS_TYPE_INVALID ||
  1349. (AddrType==ADDRESS_TYPE_MULTIPLE&&(Address&0x03000000)==0) ||
  1350. (Length!=0 && pData==NULL) || (Length==0 && AddrType==ADDRESS_TYPE_NONE) )
  1351. {
  1352. DbgFunPrint("Invalid Parameter");
  1353. *pStatus = I2C_STATUS_DATA_ERROR;
  1354. return;
  1355. }
  1356. if(in_atomic())
  1357. {
  1358. if(QueueI2CFun(MasterIndex,DeviceID,AddrType,Address,pData,Length,Speed)<0)
  1359. {
  1360. DbgFunPrint("Invalid Process!!");
  1361. *pStatus = I2C_STATUS_DATA_ERROR;
  1362. }
  1363. else
  1364. {
  1365. *pStatus = I2C_STATUS_SUCCESS;
  1366. }
  1367. return;
  1368. }
  1369. down(&I2cDev.I2cMutex[MasterIndex]);
  1370. */
  1371. /* if(down_interruptible(&I2cDev.I2cMutex[MasterIndex]))
  1372. {
  1373. DbgFunPrint("SYSTEM_ERROR");
  1374. *pStatus = I2C_STATUS_SYSTEM_ERROR;
  1375. return;
  1376. }*/
  1377. #if 0
  1378. if (bSuspend)
  1379. {
  1380. DbgFunPrint("System is suspend!!!!!!!");
  1381. // up(&I2cDev.I2cMutex[MasterIndex]);
  1382. *pStatus = I2C_STATUS_SYSTEM_ERROR;
  1383. return;
  1384. }
  1385. #endif
  1386. if(g_ucHangNum>1)
  1387. g_BusyLoop = 2;
  1388. else
  1389. g_BusyLoop = 100;
  1390. // BusyCounter = 0;
  1391. if(MasterIndex==0)
  1392. {
  1393. MxControl0 = (PVOID)M0_CONTROL0;
  1394. MxControl1 = (PVOID)M0_CONTROL1;
  1395. MxData[0] = (PVOID)M0_DATA0;
  1396. MxData[1] = (PVOID)M0_DATA1;
  1397. MxAddress = (PVOID)M0_BASE_ADDRESS;
  1398. MxDataLength = (PVOID)M0_DATA_LENGTH;
  1399. M0BusyCounter = 0;
  1400. while((MmioReadFun(M0_BUS_STATUS) & I2C_BUS_STATUS_BUSY)==I2C_BUS_STATUS_BUSY)
  1401. {
  1402. //DbgFunPrint("I2C0BusBusy %d",RegValue++);
  1403. M0BusyCounter++;
  1404. DbgFunPrint("I2C0BusBusy,BusyCount=%d",M0BusyCounter);
  1405. msleep(10);
  1406. if (M0BusyCounter > g_BusyLoop)
  1407. {
  1408. // printk(KERN_EMERG "[I2C WARNING]I2C0BusBusy!!!!!!!!!!!!\n");
  1409. *pStatus = I2C_STATUS_DATA_ERROR;
  1410. // up(&I2cDev.I2cMutex[MasterIndex]);
  1411. // if(1000 == g_BusyLoop)
  1412. {
  1413. g_ucHangNum++;
  1414. if(0==(g_ucHangNum%10))
  1415. {
  1416. printk("<0>""[I2C][%s]0_I2C BUS BUSY!!,g_ucHangNum=%d\n",__FUNCTION__,g_ucHangNum);
  1417. printk("<0>""MasterIndex = %d",MasterIndex);
  1418. printk("<0>""DeviceID = 0x%x",DeviceID);
  1419. printk("<0>""AddrType = %d",AddrType);
  1420. printk("<0>""Address = %d",Address);
  1421. printk("<0>""Length = %d",Length);
  1422. printk("<0>""Speed = %d",Speed);
  1423. }
  1424. }
  1425. return;
  1426. //break;
  1427. }
  1428. }
  1429. g_ucHangNum = 0;
  1430. }
  1431. else// if(MasterIndex==1)
  1432. {
  1433. MxControl0 = (PVOID)M1_CONTROL0;
  1434. MxControl1 = (PVOID)M1_CONTROL1;
  1435. MxData[0] = (PVOID)M1_DATA0;
  1436. MxData[1] = (PVOID)M1_DATA1;
  1437. MxAddress = (PVOID)M1_BASS_ADDRESS;
  1438. MxDataLength = (PVOID)M1_DATA_LENGTH;
  1439. M1BusyCounter = 0;
  1440. while((MmioReadFun(M1_BUS_STATUS) & I2C_BUS_STATUS_BUSY)==I2C_BUS_STATUS_BUSY)
  1441. {
  1442. //DbgFunPrint("I2C1BusBusy %d",RegValue++);
  1443. M1BusyCounter++;
  1444. DbgFunPrint("I2C1BusBusy,BusyCount=%d",M1BusyCounter);
  1445. #if 0 //UMF_OPT_INTERNETMONITOR_SPEC
  1446. #else
  1447. msleep(10);
  1448. #endif
  1449. if (M1BusyCounter > g_BusyLoop)
  1450. {
  1451. // printk(KERN_EMERG "[I2C WARNING]I2C1BusBusy!!!!!!!!!!!!\n");
  1452. *pStatus = I2C_STATUS_DATA_ERROR;
  1453. // up(&I2cDev.I2cMutex[MasterIndex]);
  1454. // if(1000 == g_BusyLoop)
  1455. {
  1456. g_ucHangNum++;
  1457. if(0==(g_ucHangNum%10))
  1458. {
  1459. printk("<0>""[I2C][%s]0_I2C BUS BUSY!!,g_ucHangNum=%d\n",__FUNCTION__,g_ucHangNum);
  1460. printk("<0>""MasterIndex = %d",MasterIndex);
  1461. printk("<0>""DeviceID = 0x%x",DeviceID);
  1462. printk("<0>""AddrType = %d",AddrType);
  1463. printk("<0>""Address = %d",Address);
  1464. printk("<0>""Length = %d",Length);
  1465. printk("<0>""Speed = %d",Speed);
  1466. }
  1467. }
  1468. return;
  1469. //break;
  1470. }
  1471. }
  1472. g_ucHangNum = 0;
  1473. }
  1474. MasterAsyncResetFun(MasterIndex,Speed); // Asynchronous reset
  1475. // To Do
  1476. Completed = 0;
  1477. if(Length == 0)
  1478. {
  1479. if(AddrType==ADDRESS_TYPE_MULTIPLE)
  1480. {
  1481. CopyBytes = (UINT8)(Address>>24)&3;
  1482. }
  1483. else
  1484. {
  1485. CopyBytes = 1;
  1486. }
  1487. Completed = CopyBytes;
  1488. Length = CopyBytes;
  1489. Remainder = 0;
  1490. memcpy(&RegValue,&Address,CopyBytes);
  1491. MmioWriteFun(MxData[0],RegValue); // Data
  1492. AddrType = ADDRESS_TYPE_NONE;
  1493. }
  1494. else
  1495. {
  1496. if(Length > 4)
  1497. {
  1498. Remainder = Length - 4;
  1499. CopyBytes = 4;
  1500. }
  1501. else
  1502. {
  1503. Remainder = 0;
  1504. CopyBytes = Length;
  1505. }
  1506. memcpy(&RegValue,&pData[Completed],CopyBytes);
  1507. Completed += CopyBytes;
  1508. MmioWriteFun(MxData[0],RegValue); // Data
  1509. if(Remainder > 0)
  1510. {
  1511. if(Remainder > 4)
  1512. {
  1513. CopyBytes = 4;
  1514. }
  1515. else
  1516. {
  1517. CopyBytes = Remainder;
  1518. }
  1519. Remainder -= CopyBytes;
  1520. memcpy(&RegValue,&pData[Completed],CopyBytes);
  1521. Completed += CopyBytes;
  1522. MmioWriteFun(MxData[1],RegValue); // Data
  1523. }
  1524. }
  1525. if(AddrType==ADDRESS_TYPE_BYTE)
  1526. {
  1527. MmioWriteFun(MxDataLength,(UINT32)Length); // Data Length
  1528. RegValue = 0x01000000 | (Address & 0xFF);
  1529. MmioWriteFun(MxAddress,RegValue); // Address
  1530. RegValue = (UINT32)DeviceID&0xFE;
  1531. MmioWriteFun(MxControl1,RegValue);
  1532. RegValue = 0x02800000;
  1533. //I2cDev.InterruptCounter[MasterIndex] = 1;
  1534. }
  1535. /* else if(AddrType==ADDRESS_TYPE_WORD)
  1536. {
  1537. RegValue = 0x02800000;
  1538. }*/
  1539. else if(AddrType==ADDRESS_TYPE_NONE)
  1540. {
  1541. MmioWriteFun(MxDataLength,(UINT32)Length); // Data Length
  1542. RegValue = (UINT32)DeviceID&0xFE;
  1543. MmioWriteFun(MxControl1,RegValue);
  1544. RegValue = 0x02800000 | MX_CONTROL0_NO_ADDRESS;
  1545. //I2cDev.InterruptCounter[MasterIndex] = 0;
  1546. }
  1547. else if(AddrType==ADDRESS_TYPE_MULTIPLE)
  1548. {
  1549. RegValue = ((UINT32)Length)<<16;
  1550. MmioWriteFun(MxDataLength,RegValue); // Data Length
  1551. MmioWriteFun(MxAddress,Address&0x03FFFFFF); // Address
  1552. RegValue = ((UINT32)DeviceID&0xFE) | (((UINT32)DeviceID&0xFE)<<16);
  1553. MmioWriteFun(MxControl1,RegValue);
  1554. RegValue = 0x03800000;
  1555. //I2cDev.InterruptCounter[MasterIndex] = 1;
  1556. }
  1557. I2cDev.InterruptCounter[MasterIndex] = 0;
  1558. if(Speed==I2C_SPEED_25K)
  1559. {
  1560. RegValue |= MX_CONTROL0_SCL_25K;
  1561. }
  1562. else if(Speed==I2C_SPEED_100K)
  1563. {
  1564. RegValue |= MX_CONTROL0_SCL_100K;
  1565. }
  1566. else if(Speed==I2C_SPEED_200K)
  1567. {
  1568. RegValue |= MX_CONTROL0_SCL_200K;
  1569. }
  1570. else if(Speed==I2C_SPEED_300K)
  1571. {
  1572. RegValue |= MX_CONTROL0_SCL_300K;
  1573. }
  1574. else if(Speed==I2C_SPEED_400K)
  1575. {
  1576. RegValue |= MX_CONTROL0_SCL_400K;
  1577. }
  1578. I2cDev.Status[MasterIndex] = I2C_STATUS_SUCCESS;
  1579. #if INTERRUPT_ENABLE
  1580. I2cDev.IntInfo[MasterIndex].pBuffer = pData;
  1581. I2cDev.IntInfo[MasterIndex].Length = Length;
  1582. I2cDev.IntInfo[MasterIndex].Completed = Completed;
  1583. I2cDev.IntInfo[MasterIndex].bRead = FALSE;
  1584. #if ONE_BYTE_INTERRUPT
  1585. I2cDev.InterruptCounter[MasterIndex] += Length;
  1586. RegValue |= MX_CONTROL0_INT_EN|MX_CONTROL0_ONE_BYTE_INT|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE;
  1587. #else
  1588. I2cDev.InterruptCounter[MasterIndex] += (Length+3)/4;
  1589. RegValue |= MX_CONTROL0_INT_EN|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE;
  1590. #endif
  1591. //if(MasterIndex==0)
  1592. // mod_timer(&I2cDev.ResetTimer[0],jiffies+(Length+49)/50*(HZ/25));
  1593. wmb();
  1594. MmioWriteFun(MxControl0,RegValue);
  1595. //20120720 DylanWang, use timeout to avoid watch dog reboot the system
  1596. //wait 2s
  1597. //wait_for_completion(&I2cDev.Comp[MasterIndex]);
  1598. ucStatus = wait_for_completion_timeout(&I2cDev.Comp[MasterIndex], 2*HZ);
  1599. if(ucStatus<2)
  1600. {
  1601. g_ucHangNum++;
  1602. printk("<0>""[I2C][%s]wait completion timeout!!\n",__FUNCTION__);
  1603. *pStatus = I2C_STATUS_SYSTEM_TIMEOUT;
  1604. return;
  1605. }
  1606. #if 0
  1607. if (bSuspend)
  1608. {
  1609. DbgFunPrint("System is suspend!!!!!!!");
  1610. // up(&I2cDev.I2cMutex[MasterIndex]);
  1611. *pStatus = I2C_STATUS_SYSTEM_ERROR;
  1612. return;
  1613. }
  1614. #endif
  1615. MmioWriteFun(0xbe070054,500); //20110120 lwhite add 20us delay between two commands (stop and next start)
  1616. MmioWriteFun(0xbe110054,500);
  1617. /* if(I2cDev.Status[MasterIndex]!=I2C_STATUS_SUCCESS)
  1618. {
  1619. DbgFunPrint("I2C Write %d/%d",I2cDev.IntInfo[MasterIndex].Completed,Length);
  1620. }*/
  1621. #else // else of #if INTERRUPT_ENABLE
  1622. I2cDev.InterruptCounter[MasterIndex] += Length;
  1623. RegValue |= MX_CONTROL0_ONE_BYTE_INT;
  1624. wmb();
  1625. MmioWriteFun(MxControl0,RegValue);
  1626. if(Length>4)
  1627. {
  1628. if(AddrType==ADDRESS_TYPE_NONE)
  1629. {
  1630. MasterPollingFun(MasterIndex,4);
  1631. }
  1632. else
  1633. {
  1634. MasterPollingFun(MasterIndex,5);
  1635. }
  1636. }
  1637. else
  1638. {
  1639. if(AddrType==ADDRESS_TYPE_NONE)
  1640. {
  1641. MasterPollingFun(MasterIndex,Length);
  1642. }
  1643. else
  1644. {
  1645. MasterPollingFun(MasterIndex,Length+1);
  1646. }
  1647. }
  1648. while(Remainder > 0)
  1649. {
  1650. if(Remainder>4)
  1651. {
  1652. CopyBytes = 4;
  1653. }
  1654. else
  1655. {
  1656. CopyBytes = Remainder;
  1657. }
  1658. memcpy(&RegValue,&pData[Completed],CopyBytes);
  1659. MmioWriteFun(MxData[Switch],RegValue); // Data
  1660. Switch ^= 1;
  1661. Remainder -= CopyBytes;
  1662. Completed += CopyBytes;
  1663. MasterPollingFun(MasterIndex,CopyBytes);
  1664. }
  1665. MasterPollingFun(MasterIndex,I2cDev.InterruptCounter[MasterIndex]);
  1666. #endif // end of #if INTERRUPT_ENABLE
  1667. *pStatus = I2cDev.Status[MasterIndex];
  1668. }
  1669. void DRV_I2C_WriteFun( UINT8 MasterIndex,
  1670. UINT8 DeviceID ,
  1671. UINT8 AddrType ,
  1672. UINT32 Address ,
  1673. UINT8 *pData ,
  1674. UINT16 Length ,
  1675. UINT8 Speed ,
  1676. UINT8 *pStatus)
  1677. {
  1678. if(MasterIndex>=MASTER_NUMBER || AddrType>=ADDRESS_TYPE_INVALID ||
  1679. (AddrType==ADDRESS_TYPE_MULTIPLE&&(Address&0x03000000)==0) ||
  1680. (Length!=0 && pData==NULL) || (Length==0 && AddrType==ADDRESS_TYPE_NONE) )
  1681. {
  1682. DbgFunPrint("Invalid Parameter");
  1683. *pStatus = I2C_STATUS_DATA_ERROR;
  1684. return;
  1685. }
  1686. if(in_atomic())
  1687. {
  1688. if(QueueI2CFun(MasterIndex,DeviceID,AddrType,Address,pData,Length,Speed)<0)
  1689. {
  1690. DbgFunPrint("Invalid Process!!");
  1691. *pStatus = I2C_STATUS_DATA_ERROR;
  1692. }
  1693. else
  1694. {
  1695. *pStatus = I2C_STATUS_SUCCESS;
  1696. }
  1697. return;
  1698. }
  1699. down(&I2cDev.I2cMutex[MasterIndex]);
  1700. I2CWrite(MasterIndex,DeviceID,AddrType,Address,pData,Length,Speed,pStatus);
  1701. up(&I2cDev.I2cMutex[MasterIndex]);
  1702. }
  1703. UINT32 DRV_I2C_M0BasicReadFun(UINT8 DeviceID,UINT8 Address,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
  1704. {
  1705. UINT32 RegValue;
  1706. // DebugPrint("DRV_I2C_M0BasicReadFun 0x%02x 0x%02x %d %d",DeviceID,Address,Length,Speed);
  1707. if(Length>4)
  1708. {
  1709. *pStatus = I2C_STATUS_DATA_ERROR;
  1710. DbgFunPrint("Invalid Parameter");
  1711. return -1;
  1712. }
  1713. DRV_I2C_ReadFun(0,DeviceID,ADDRESS_TYPE_BYTE,Address,(UINT8*)(&RegValue),(UINT32)(Length),Speed,(UINT8*)(pStatus));
  1714. return RegValue;
  1715. }
  1716. void DRV_I2C_M0BasicWriteFun(UINT8 DeviceID,UINT8 Address,UINT32 Data,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
  1717. {
  1718. // DebugPrint("DRV_I2C_M0BasicWriteFun 0x%02x 0x%02x 0x%08x %d %d",DeviceID,Address,Data,Length,Speed);
  1719. if(Length>4)
  1720. {
  1721. *pStatus = I2C_STATUS_DATA_ERROR;
  1722. DbgFunPrint("Invalid Parameter");
  1723. return;
  1724. }
  1725. DRV_I2C_WriteFun(0,DeviceID,ADDRESS_TYPE_BYTE,(UINT32)(Address),(UINT8*)(&Data),(UINT16)(Length),Speed,(UINT8*)(pStatus));
  1726. }
  1727. UINT32 DRV_I2C_M1BasicReadFun(UINT8 DeviceID,UINT8 Address,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
  1728. {
  1729. UINT32 RegValue;
  1730. // DebugPrint("DRV_I2C_M1BasicReadFun 0x%02x 0x%02x %d %d",DeviceID,Address,Length,Speed);
  1731. if(Length>4)
  1732. {
  1733. *pStatus = I2C_STATUS_DATA_ERROR;
  1734. DbgFunPrint("Invalid Parameter");
  1735. return -1;
  1736. }
  1737. DRV_I2C_ReadFun(1,DeviceID,ADDRESS_TYPE_BYTE,Address,(UINT8*)(&RegValue),(UINT32)(Length),Speed,(UINT8*)(pStatus));
  1738. return RegValue;
  1739. }
  1740. void DRV_I2C_M1BasicWriteFun(UINT8 DeviceID,UINT8 Address,UINT32 Data,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
  1741. {
  1742. // DebugPrint("DRV_I2C_M1BasicWriteFun 0x%02x 0x%02x 0x%08x %d %d",DeviceID,Address,Data,Length,Speed);
  1743. if(Length>4)
  1744. {
  1745. *pStatus = I2C_STATUS_DATA_ERROR;
  1746. DbgFunPrint("Invalid Parameter");
  1747. return;
  1748. }
  1749. DRV_I2C_WriteFun(1,DeviceID,ADDRESS_TYPE_BYTE,(UINT32)(Address),(UINT8*)(&Data),(UINT16)(Length),Speed,(UINT8*)(pStatus));
  1750. }
  1751. #endif
  1752. INT32 DRV_I2C_QueueCmdFun(UINT8 Mx,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed, INT8 bRead)
  1753. {
  1754. PI2C_CMD_LIST pI2CCmdList;
  1755. //printk(KERN_INFO "[lwhite] DRV_I2C_QueueCmdFun===>\n");
  1756. pI2CCmdList = drv_kmalloc(sizeof(I2C_CMD_LIST)+Length-1,GFP_ATOMIC, MODULEID_I2C); // Cannot sleep
  1757. if(!pI2CCmdList)
  1758. {
  1759. DbgFunPrint("Cannot allocate I2C_CMD_LIST");
  1760. return -ENOMEM;
  1761. }
  1762. if (bRead)
  1763. {
  1764. pI2CCmdList->bRead = bRead;
  1765. pI2CCmdList->Mx = Mx;
  1766. pI2CCmdList->DeviceID = DeviceID;
  1767. pI2CCmdList->AddrType = AddrType;
  1768. pI2CCmdList->Speed = Speed;
  1769. pI2CCmdList->Address = Address;
  1770. pI2CCmdList->Length = Length;
  1771. }
  1772. else
  1773. {
  1774. pI2CCmdList->bRead = bRead;
  1775. pI2CCmdList->Mx = Mx;
  1776. pI2CCmdList->DeviceID = DeviceID;
  1777. pI2CCmdList->AddrType = AddrType;
  1778. pI2CCmdList->Speed = Speed;
  1779. pI2CCmdList->Address = Address;
  1780. pI2CCmdList->Length = Length;
  1781. memcpy(pI2CCmdList->Data,pData,Length);
  1782. }
  1783. list_add_tail(&pI2CCmdList->list, &CmdList);
  1784. //printk(KERN_INFO "[lwhite] DRV_I2C_QueueCmdFun<===\n");
  1785. return 0;
  1786. }
  1787. INT32 DRV_I2C_RunCmdFun(UINT8 Mx,UINT8 *pData)
  1788. {
  1789. struct list_head QueueList,*cursor = NULL,*next = NULL;
  1790. PI2C_CMD_LIST pQueue;
  1791. UINT8 Status;
  1792. INT8 bLast = 0;
  1793. INT8 result= 0;
  1794. //printk(KERN_INFO "[lwhite] DRV_I2C_RunCmdFun===>\n");
  1795. if (Mx > 1)
  1796. {
  1797. DbgFunPrint("[lwhite] DRV_I2C_RunCmdFun ERROR===> Mx error!!!!");
  1798. result = 1;
  1799. return result;
  1800. }
  1801. down(&I2cDev.I2cMutex[Mx]);
  1802. INIT_LIST_HEAD(&QueueList);
  1803. list_splice_init(&CmdList,&QueueList);
  1804. list_for_each_safe(cursor,next,&QueueList)
  1805. {
  1806. if (cmd_list_is_last(cursor,&QueueList))
  1807. {
  1808. //printk(KERN_INFO "[lwhite] DRV_I2C_RunCmdFun===>this is the last one!!!!\n");
  1809. bLast = 1;
  1810. }
  1811. list_del(cursor);
  1812. pQueue = container_of(cursor,I2C_CMD_LIST,list);
  1813. //Status = I2C_STATUS_SUCCESS;
  1814. if (pQueue->bRead && bLast)
  1815. {
  1816. I2CRead(pQueue->Mx
  1817. ,pQueue->DeviceID
  1818. ,pQueue->AddrType
  1819. ,pQueue->Address
  1820. ,pQueue->Data,pQueue->Length
  1821. ,pQueue->Speed,&Status);
  1822. if (Status)
  1823. {
  1824. result = 1;
  1825. DbgFunPrint("[lwhite] DRV_I2C_RunCmdFun ERROR===> Read Status error!!!!");
  1826. }
  1827. //printk(KERN_EMERG "[lwhite] DRV_I2C_RunCmdFun===> pQueue->Data[0]=0x%02x\n",pQueue->Data[0]);
  1828. memcpy(pData,&pQueue->Data,pQueue->Length);
  1829. //printk(KERN_EMERG "[lwhite] DRV_I2C_RunCmdFun===> pData[0]=0x%02x\n",pData[0]);
  1830. }
  1831. else if (pQueue->bRead && bLast==0)
  1832. {
  1833. DbgFunPrint("[lwhite] DRV_I2C_RunCmdFun ERROR===> the Read Command is not the last one!!!!");
  1834. result = 1;
  1835. }
  1836. else
  1837. {
  1838. I2CWrite(pQueue->Mx
  1839. ,pQueue->DeviceID
  1840. ,pQueue->AddrType
  1841. ,pQueue->Address
  1842. ,pQueue->Data,pQueue->Length
  1843. ,pQueue->Speed,&Status);
  1844. if (Status)
  1845. {
  1846. result = 1;
  1847. DbgFunPrint("[lwhite] DRV_I2C_RunCmdFun ERROR===> Write Status error!!!!");
  1848. }
  1849. }
  1850. drv_kfree(pQueue, MODULEID_I2C);
  1851. }
  1852. //printk(KERN_INFO "[lwhite] DRV_I2C_RunCmdFun<===\n");
  1853. up(&I2cDev.I2cMutex[Mx]);
  1854. return result;
  1855. }
  1856. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,8)
  1857. static long I2cIoctlFun(struct file *file, unsigned int cmd, unsigned long arg)
  1858. #else
  1859. static int I2cIoctlFun(struct inode *inode, struct file *pFile,unsigned int cmd, unsigned long arg)
  1860. #endif
  1861. {
  1862. // PI2C_DEV pDev;
  1863. INT32 retval;
  1864. UINT8 Status;
  1865. // DebugPrint("I2cIoctlFun");
  1866. if (_IOC_TYPE(cmd) != I2C_IOC_MAGIC)
  1867. {
  1868. DbgFunPrint("Invalid command");
  1869. return -ENOTTY;
  1870. }
  1871. // pDev = (PI2C_DEV)pFile->private_data;
  1872. retval = 0;
  1873. Status = 0;
  1874. switch(cmd)
  1875. {
  1876. //#ifndef RELEASE_VER
  1877. case IOC_I2C_MASTER0_WRITE_BYTE_100K:
  1878. {
  1879. UINT8 Data[3];
  1880. // DebugPrint("IOC_I2C_MASTER0_WRITE_BYTE_100K");
  1881. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  1882. {
  1883. DbgFunPrint("copy_from_user fail");
  1884. retval = -EFAULT;
  1885. break;
  1886. }
  1887. DRV_I2C_M0_WRITE_BYTE_100K(Data[0],Data[1],Data[2],&Status);
  1888. if(Status)
  1889. {
  1890. DbgFunPrint("Status Error:%d",Status);
  1891. retval = -EFAULT;
  1892. break;
  1893. }
  1894. break;
  1895. }
  1896. case IOC_I2C_MASTER0_READ_BYTE_100K:
  1897. {
  1898. UINT8 Data[2];
  1899. // DebugPrint("IOC_I2C_MASTER0_READ_BYTE_100K");
  1900. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  1901. {
  1902. DbgFunPrint("copy_from_user fail");
  1903. retval = -EFAULT;
  1904. break;
  1905. }
  1906. Data[0] = DRV_I2C_M0_READ_BYTE_100K(Data[0],Data[1],&Status);
  1907. if(Status)
  1908. {
  1909. DbgFunPrint("Status Error:%d",Status);
  1910. retval = -EFAULT;
  1911. break;
  1912. }
  1913. if( put_user(Data[0],(UINT8 __user *)arg) )
  1914. {
  1915. DbgFunPrint("put_user fail");
  1916. retval = -EFAULT;
  1917. break;
  1918. }
  1919. break;
  1920. }
  1921. case IOC_I2C_MASTER0_WRITE_BYTE_400K:
  1922. {
  1923. UINT8 Data[3];
  1924. // DebugPrint("IOC_I2C_MASTER0_WRITE_BYTE_400K");
  1925. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  1926. {
  1927. DbgFunPrint("copy_from_user fail");
  1928. retval = -EFAULT;
  1929. break;
  1930. }
  1931. DRV_I2C_M0_WRITE_BYTE_400K(Data[0],Data[1],Data[2],&Status);
  1932. if(Status)
  1933. {
  1934. DbgFunPrint("Status Error:%d",Status);
  1935. retval = -EFAULT;
  1936. break;
  1937. }
  1938. break;
  1939. }
  1940. case IOC_I2C_MASTER0_READ_BYTE_400K:
  1941. {
  1942. UINT8 Data[2];
  1943. // DebugPrint("IOC_I2C_MASTER0_READ_BYTE_400K");
  1944. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  1945. {
  1946. DbgFunPrint("copy_from_user fail");
  1947. retval = -EFAULT;
  1948. break;
  1949. }
  1950. Data[0] = DRV_I2C_M0_READ_BYTE_400K(Data[0],Data[1],&Status);
  1951. if(Status)
  1952. {
  1953. DbgFunPrint("Status Error:%d",Status);
  1954. retval = -EFAULT;
  1955. break;
  1956. }
  1957. if( put_user(Data[0],(UINT8 __user *)arg) )
  1958. {
  1959. DbgFunPrint("put_user fail");
  1960. retval = -EFAULT;
  1961. break;
  1962. }
  1963. break;
  1964. }
  1965. case IOC_I2C_MASTER1_WRITE_BYTE_100K:
  1966. {
  1967. UINT8 Data[3];
  1968. // DebugPrint("IOC_I2C_MASTER1_WRITE_BYTE_100K");
  1969. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  1970. {
  1971. DbgFunPrint("copy_from_user fail");
  1972. retval = -EFAULT;
  1973. break;
  1974. }
  1975. DRV_I2C_M1_WRITE_BYTE_100K(Data[0],Data[1],Data[2],&Status);
  1976. if(Status)
  1977. {
  1978. DbgFunPrint("Status Error:%d",Status);
  1979. retval = -EFAULT;
  1980. break;
  1981. }
  1982. break;
  1983. }
  1984. case IOC_I2C_MASTER1_READ_BYTE_100K:
  1985. {
  1986. UINT8 Data[2];
  1987. // DebugPrint("IOC_I2C_MASTER1_READ_BYTE_100K");
  1988. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  1989. {
  1990. DbgFunPrint("copy_from_user fail");
  1991. retval = -EFAULT;
  1992. break;
  1993. }
  1994. Data[0] = DRV_I2C_M1_READ_BYTE_100K(Data[0],Data[1],&Status);
  1995. if(Status)
  1996. {
  1997. DbgFunPrint("Status Error:%d",Status);
  1998. retval = -EFAULT;
  1999. break;
  2000. }
  2001. if( put_user(Data[0],(UINT8 __user *)arg) )
  2002. {
  2003. DbgFunPrint("put_user fail");
  2004. retval = -EFAULT;
  2005. break;
  2006. }
  2007. break;
  2008. }
  2009. case IOC_I2C_MASTER0_WRITE_WORD_100K:
  2010. {
  2011. UINT8 Data[4];
  2012. // DebugPrint("IOC_I2C_MASTER0_WRITE_WORD_100K");
  2013. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2014. {
  2015. DbgFunPrint("copy_from_user fail");
  2016. retval = -EFAULT;
  2017. break;
  2018. }
  2019. DRV_I2C_M0_WRITE_WORD_100K(Data[0],Data[1],*((UINT16 *)(&Data[2])),&Status);
  2020. if(Status)
  2021. {
  2022. DbgFunPrint("Status Error:%d",Status);
  2023. retval = -EFAULT;
  2024. break;
  2025. }
  2026. break;
  2027. }
  2028. case IOC_I2C_MASTER0_READ_WORD_100K:
  2029. {
  2030. UINT16 Data;
  2031. // DebugPrint("IOC_I2C_MASTER0_READ_WORD_100K");
  2032. if( get_user(Data,(UINT16 __user *)arg) )
  2033. {
  2034. DbgFunPrint("get_user fail");
  2035. retval = -EFAULT;
  2036. break;
  2037. }
  2038. Data = DRV_I2C_M0_READ_WORD_100K((UINT8)Data,(UINT8)(Data>>8),&Status);
  2039. if(Status)
  2040. {
  2041. DbgFunPrint("Status Error:%d",Status);
  2042. retval = -EFAULT;
  2043. break;
  2044. }
  2045. if( put_user(Data,(UINT16 __user *)arg) )
  2046. {
  2047. DbgFunPrint("put_user fail");
  2048. retval = -EFAULT;
  2049. break;
  2050. }
  2051. break;
  2052. }
  2053. case IOC_I2C_MASTER0_WRITE_DWORD_100K:
  2054. {
  2055. UINT8 Data[6];
  2056. // DebugPrint("IOC_I2C_MASTER0_WRITE_DWORD_100K");
  2057. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2058. {
  2059. DbgFunPrint("copy_from_user fail");
  2060. retval = -EFAULT;
  2061. break;
  2062. }
  2063. DRV_I2C_M0_WRITE_DWORD_100K(Data[0],Data[1],*((UINT32 *)(&Data[2])),&Status);
  2064. if(Status)
  2065. {
  2066. DbgFunPrint("Status Error:%d",Status);
  2067. retval = -EFAULT;
  2068. break;
  2069. }
  2070. break;
  2071. }
  2072. case IOC_I2C_MASTER0_READ_DWORD_100K:
  2073. {
  2074. UINT32 Data;
  2075. // DebugPrint("IOC_I2C_MASTER0_READ_DWORD_100K");
  2076. if( get_user(Data,(UINT32 __user *)arg) )
  2077. {
  2078. DbgFunPrint("get_user fail");
  2079. retval = -EFAULT;
  2080. break;
  2081. }
  2082. Data = DRV_I2C_M0_READ_DWORD_100K((UINT8)Data,(UINT8)(Data>>8),&Status);
  2083. if(Status)
  2084. {
  2085. DbgFunPrint("Status Error:%d",Status);
  2086. retval = -EFAULT;
  2087. break;
  2088. }
  2089. if( put_user(Data,(UINT32 __user *)arg) )
  2090. {
  2091. DbgFunPrint("put_user fail");
  2092. retval = -EFAULT;
  2093. break;
  2094. }
  2095. break;
  2096. }
  2097. case IOC_I2C_MASTER0_WRITE_WORD_400K:
  2098. {
  2099. UINT8 Data[4];
  2100. // DebugPrint("IOC_I2C_MASTER0_WRITE_WORD_400K");
  2101. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2102. {
  2103. DbgFunPrint("copy_from_user fail");
  2104. retval = -EFAULT;
  2105. break;
  2106. }
  2107. DRV_I2C_M0_WRITE_WORD_400K(Data[0],Data[1],*((UINT16 *)(&Data[2])),&Status);
  2108. if(Status)
  2109. {
  2110. DbgFunPrint("Status Error:%d",Status);
  2111. retval = -EFAULT;
  2112. break;
  2113. }
  2114. break;
  2115. }
  2116. case IOC_I2C_MASTER0_READ_WORD_400K:
  2117. {
  2118. UINT16 Data;
  2119. // DebugPrint("IOC_I2C_MASTER0_READ_WORD_400K");
  2120. if( get_user(Data,(UINT16 __user *)arg) )
  2121. {
  2122. DbgFunPrint("get_user fail");
  2123. retval = -EFAULT;
  2124. break;
  2125. }
  2126. Data = DRV_I2C_M0_READ_WORD_400K((UINT8)Data,(UINT8)(Data>>8),&Status);
  2127. if(Status)
  2128. {
  2129. DbgFunPrint("Status Error:%d",Status);
  2130. retval = -EFAULT;
  2131. break;
  2132. }
  2133. if( put_user(Data,(UINT16 __user *)arg) )
  2134. {
  2135. DbgFunPrint("put_user fail");
  2136. retval = -EFAULT;
  2137. break;
  2138. }
  2139. break;
  2140. }
  2141. case IOC_I2C_MASTER0_WRITE_DWORD_400K:
  2142. {
  2143. UINT8 Data[6];
  2144. // DebugPrint("IOC_I2C_MASTER0_WRITE_DWORD_400K");
  2145. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2146. {
  2147. DbgFunPrint("copy_from_user fail");
  2148. retval = -EFAULT;
  2149. break;
  2150. }
  2151. DRV_I2C_M0_WRITE_DWORD_400K(Data[0],Data[1],*((UINT32 *)(&Data[2])),&Status);
  2152. if(Status)
  2153. {
  2154. DbgFunPrint("Status Error:%d",Status);
  2155. retval = -EFAULT;
  2156. break;
  2157. }
  2158. break;
  2159. }
  2160. case IOC_I2C_MASTER0_READ_DWORD_400K:
  2161. {
  2162. UINT32 Data;
  2163. // DebugPrint("IOC_I2C_MASTER0_READ_DWORD_400K");
  2164. if( get_user(Data,(UINT32 __user *)arg) )
  2165. {
  2166. DbgFunPrint("get_user fail");
  2167. retval = -EFAULT;
  2168. break;
  2169. }
  2170. Data = DRV_I2C_M0_READ_DWORD_400K((UINT8)Data,(UINT8)(Data>>8),&Status);
  2171. if(Status)
  2172. {
  2173. DbgFunPrint("Status Error:%d",Status);
  2174. retval = -EFAULT;
  2175. break;
  2176. }
  2177. if( put_user(Data,(UINT32 __user *)arg) )
  2178. {
  2179. DbgFunPrint("put_user fail");
  2180. retval = -EFAULT;
  2181. break;
  2182. }
  2183. break;
  2184. }
  2185. case IOC_I2C_MASTER1_WRITE_WORD_100K:
  2186. {
  2187. UINT8 Data[4];
  2188. // DebugPrint("IOC_I2C_MASTER1_WRITE_WORD_100K");
  2189. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2190. {
  2191. DbgFunPrint("copy_from_user fail");
  2192. retval = -EFAULT;
  2193. break;
  2194. }
  2195. DRV_I2C_M1_WRITE_WORD_100K(Data[0],Data[1],*((UINT16 *)(&Data[2])),&Status);
  2196. if(Status)
  2197. {
  2198. DbgFunPrint("Status Error:%d",Status);
  2199. retval = -EFAULT;
  2200. break;
  2201. }
  2202. break;
  2203. }
  2204. case IOC_I2C_MASTER1_READ_WORD_100K:
  2205. {
  2206. UINT16 Data;
  2207. // DebugPrint("IOC_I2C_MASTER1_READ_WORD_100K");
  2208. if( get_user(Data,(UINT16 __user *)arg) )
  2209. {
  2210. DbgFunPrint("get_user fail");
  2211. retval = -EFAULT;
  2212. break;
  2213. }
  2214. Data = DRV_I2C_M1_READ_WORD_100K((UINT8)Data,(UINT8)(Data>>8),&Status);
  2215. if(Status)
  2216. {
  2217. DbgFunPrint("Status Error:%d",Status);
  2218. retval = -EFAULT;
  2219. break;
  2220. }
  2221. if( put_user(Data,(UINT16 __user *)arg) )
  2222. {
  2223. DbgFunPrint("put_user fail");
  2224. retval = -EFAULT;
  2225. break;
  2226. }
  2227. break;
  2228. }
  2229. case IOC_I2C_MASTER1_WRITE_DWORD_100K:
  2230. {
  2231. UINT8 Data[6];
  2232. // DebugPrint("IOC_I2C_MASTER1_WRITE_DWORD_100K");
  2233. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2234. {
  2235. DbgFunPrint("copy_from_user fail");
  2236. retval = -EFAULT;
  2237. break;
  2238. }
  2239. DRV_I2C_M1_WRITE_DWORD_100K(Data[0],Data[1],*((UINT32 *)(&Data[2])),&Status);
  2240. if(Status)
  2241. {
  2242. DbgFunPrint("Status Error:%d",Status);
  2243. retval = -EFAULT;
  2244. break;
  2245. }
  2246. break;
  2247. }
  2248. case IOC_I2C_MASTER1_READ_DWORD_100K:
  2249. {
  2250. UINT32 Data;
  2251. // DebugPrint("IOC_I2C_MASTER1_READ_DWORD_100K");
  2252. if( get_user(Data,(UINT32 __user *)arg) )
  2253. {
  2254. DbgFunPrint("get_user fail");
  2255. retval = -EFAULT;
  2256. break;
  2257. }
  2258. Data = DRV_I2C_M1_READ_DWORD_100K((UINT8)Data,(UINT8)(Data>>8),&Status);
  2259. if(Status)
  2260. {
  2261. DbgFunPrint("Status Error:%d",Status);
  2262. retval = -EFAULT;
  2263. break;
  2264. }
  2265. if( put_user(Data,(UINT32 __user *)arg) )
  2266. {
  2267. DbgFunPrint("put_user fail");
  2268. retval = -EFAULT;
  2269. break;
  2270. }
  2271. break;
  2272. }
  2273. case IOC_I2C_MASTER1_WRITE_BYTE_400K:
  2274. {
  2275. UINT8 Data[3];
  2276. // DebugPrint("IOC_I2C_MASTER1_WRITE_BYTE_400K");
  2277. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2278. {
  2279. DbgFunPrint("copy_from_user fail");
  2280. retval = -EFAULT;
  2281. break;
  2282. }
  2283. DRV_I2C_M1_WRITE_BYTE_400K(Data[0],Data[1],Data[2],&Status);
  2284. if(Status)
  2285. {
  2286. DbgFunPrint("Status Error:%d",Status);
  2287. retval = -EFAULT;
  2288. break;
  2289. }
  2290. break;
  2291. }
  2292. case IOC_I2C_MASTER1_READ_BYTE_400K:
  2293. {
  2294. UINT8 Data[2];
  2295. // DebugPrint("IOC_I2C_MASTER1_READ_BYTE_400K");
  2296. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2297. {
  2298. DbgFunPrint("copy_from_user fail");
  2299. retval = -EFAULT;
  2300. break;
  2301. }
  2302. Data[0] = DRV_I2C_M1_READ_BYTE_400K(Data[0],Data[1],&Status);
  2303. if(Status)
  2304. {
  2305. DbgFunPrint("Status Error:%d",Status);
  2306. retval = -EFAULT;
  2307. break;
  2308. }
  2309. if( put_user(Data[0],(UINT8 __user *)arg) )
  2310. {
  2311. DbgFunPrint("put_user fail");
  2312. retval = -EFAULT;
  2313. break;
  2314. }
  2315. break;
  2316. }
  2317. case IOC_I2C_MASTER1_WRITE_WORD_400K:
  2318. {
  2319. UINT8 Data[4];
  2320. // DebugPrint("IOC_I2C_MASTER1_WRITE_WORD_400K");
  2321. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2322. {
  2323. DbgFunPrint("copy_from_user fail");
  2324. retval = -EFAULT;
  2325. break;
  2326. }
  2327. DRV_I2C_M1_WRITE_WORD_400K(Data[0],Data[1],*((UINT16 *)(&Data[2])),&Status);
  2328. if(Status)
  2329. {
  2330. DbgFunPrint("Status Error:%d",Status);
  2331. retval = -EFAULT;
  2332. break;
  2333. }
  2334. break;
  2335. }
  2336. case IOC_I2C_MASTER1_READ_WORD_400K:
  2337. {
  2338. UINT16 Data;
  2339. // DebugPrint("IOC_I2C_MASTER1_READ_WORD_400K");
  2340. if( get_user(Data,(UINT16 __user *)arg) )
  2341. {
  2342. DbgFunPrint("get_user fail");
  2343. retval = -EFAULT;
  2344. break;
  2345. }
  2346. Data = DRV_I2C_M1_READ_WORD_400K((UINT8)Data,(UINT8)(Data>>8),&Status);
  2347. if(Status)
  2348. {
  2349. DbgFunPrint("Status Error:%d",Status);
  2350. retval = -EFAULT;
  2351. break;
  2352. }
  2353. if( put_user(Data,(UINT16 __user *)arg) )
  2354. {
  2355. DbgFunPrint("put_user fail");
  2356. retval = -EFAULT;
  2357. break;
  2358. }
  2359. break;
  2360. }
  2361. case IOC_I2C_MASTER1_WRITE_DWORD_400K:
  2362. {
  2363. UINT8 Data[6];
  2364. // DebugPrint("IOC_I2C_MASTER1_WRITE_DWORD_400K");
  2365. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2366. {
  2367. DbgFunPrint("copy_from_user fail");
  2368. retval = -EFAULT;
  2369. break;
  2370. }
  2371. DRV_I2C_M1_WRITE_DWORD_400K(Data[0],Data[1],*((UINT32 *)(&Data[2])),&Status);
  2372. if(Status)
  2373. {
  2374. DbgFunPrint("Status Error:%d",Status);
  2375. retval = -EFAULT;
  2376. break;
  2377. }
  2378. break;
  2379. }
  2380. case IOC_I2C_MASTER1_READ_DWORD_400K:
  2381. {
  2382. UINT32 Data;
  2383. // DebugPrint("IOC_I2C_MASTER1_READ_DWORD_400K");
  2384. if( get_user(Data,(UINT32 __user *)arg) )
  2385. {
  2386. DbgFunPrint("get_user fail");
  2387. retval = -EFAULT;
  2388. break;
  2389. }
  2390. Data = DRV_I2C_M1_READ_DWORD_400K((UINT8)Data,(UINT8)(Data>>8),&Status);
  2391. if(Status)
  2392. {
  2393. DbgFunPrint("Status Error:%d",Status);
  2394. retval = -EFAULT;
  2395. break;
  2396. }
  2397. if( put_user(Data,(UINT32 __user *)arg) )
  2398. {
  2399. DbgFunPrint("put_user fail");
  2400. retval = -EFAULT;
  2401. break;
  2402. }
  2403. break;
  2404. }
  2405. #if SOFTWARE_MODE_MASTER
  2406. #if 0
  2407. case IOC_SW_I2C_MASTER0_WRITE_BYTE:
  2408. {
  2409. UINT8 Data[3];
  2410. // DebugPrint("IOC_SW_I2C_MASTER0_WRITE_BYTE");
  2411. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2412. {
  2413. DbgFunPrint("copy_from_user fail");
  2414. retval = -EFAULT;
  2415. break;
  2416. }
  2417. SwI2CWriteBasicFun(0,Data[0],Data[1],Data[2],1,I2C_SPEED_100K,&Status);
  2418. break;
  2419. }
  2420. case IOC_SW_I2C_MASTER0_READ_BYTE:
  2421. {
  2422. UINT8 Data[2];
  2423. // DebugPrint("IOC_SW_I2C_MASTER0_READ_BYTE");
  2424. if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
  2425. {
  2426. DbgFunPrint("copy_from_user fail");
  2427. retval = -EFAULT;
  2428. break;
  2429. }
  2430. Data[0] = SwI2CReadBasicFun(0,Data[0],Data[1],1,I2C_SPEED_100K,&Status);
  2431. if( put_user(Data[0],(UINT8 __user *)arg) )
  2432. {
  2433. DbgFunPrint("put_user fail");
  2434. retval = -EFAULT;
  2435. break;
  2436. }
  2437. break;
  2438. }
  2439. case IOC_SW_I2C_MASTER_READ_TEMP: // 20081112 lwhite test
  2440. {
  2441. I2C_TEMP_INFO I2cTempInfo;
  2442. UINT8 *pData;
  2443. if(copy_from_user(&I2cTempInfo, (void __user *)arg, sizeof(I2C_TEMP_INFO)))
  2444. {
  2445. DbgFunPrint("copy_from_user fail");
  2446. retval = -EFAULT;
  2447. break;
  2448. }
  2449. pData = drv_kmalloc(I2cTempInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
  2450. if(!pData)
  2451. {
  2452. DbgFunPrint("No resource");
  2453. retval = -ENOMEM;
  2454. break;
  2455. }
  2456. // memset(pData,0,I2cInfo.DataLength);
  2457. I2CReadTempFun(I2cTempInfo.MasterIndex
  2458. ,I2cTempInfo.SlaveAddress // DeviceID
  2459. ,I2cTempInfo.BaseAddrType
  2460. ,I2cTempInfo.BaseAddress // Offset
  2461. ,I2cTempInfo.AddrLength
  2462. ,pData, I2cTempInfo.DataLength
  2463. ,I2cTempInfo.Speed
  2464. ,&Status);
  2465. if(Status)
  2466. {
  2467. DbgFunPrint("Status Error:%d",Status);
  2468. retval = -EFAULT;
  2469. drv_kfree(pData, MODULEID_I2C);
  2470. break;
  2471. }
  2472. if(copy_to_user((void __user *)I2cTempInfo.Data,pData,I2cTempInfo.DataLength))
  2473. {
  2474. DbgFunPrint("__copy_to_user fail");
  2475. retval = -EFAULT;
  2476. drv_kfree(pData, MODULEID_I2C);
  2477. break;
  2478. }
  2479. drv_kfree(pData, MODULEID_I2C);
  2480. break;
  2481. }
  2482. #endif
  2483. case IOC_SW_I2C_MASTER_READ:
  2484. {
  2485. I2C_INFO I2cInfo;
  2486. UINT8 *pData;
  2487. if(copy_from_user(&I2cInfo, (void __user *)arg, sizeof(I2C_INFO)))
  2488. {
  2489. DbgFunPrint("copy_from_user fail");
  2490. retval = -EFAULT;
  2491. break;
  2492. }
  2493. pData = drv_kmalloc(I2cInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
  2494. if(!pData)
  2495. {
  2496. DbgFunPrint("No resource");
  2497. retval = -ENOMEM;
  2498. break;
  2499. }
  2500. // memset(pData,0,I2cInfo.DataLength);
  2501. DRV_SwI2C_ReadFun(I2cInfo.MasterIndex
  2502. ,I2cInfo.SlaveAddress // DeviceID
  2503. ,I2cInfo.BaseAddrType
  2504. ,I2cInfo.BaseAddress // Offset
  2505. ,pData, I2cInfo.DataLength
  2506. ,I2cInfo.Speed
  2507. ,&Status);
  2508. // copy_to_user((void __user *)&I2cInfo.Status,Status,sizeof(Status));
  2509. if(Status)
  2510. {
  2511. DbgFunPrint("Status Error:%d",Status);
  2512. retval = -EFAULT;
  2513. drv_kfree(pData, MODULEID_I2C);
  2514. break;
  2515. }
  2516. if(copy_to_user((void __user *)I2cInfo.Data,pData,I2cInfo.DataLength))
  2517. {
  2518. DbgFunPrint("__copy_to_user fail");
  2519. retval = -EFAULT;
  2520. drv_kfree(pData, MODULEID_I2C);
  2521. break;
  2522. }
  2523. drv_kfree(pData, MODULEID_I2C);
  2524. break;
  2525. }
  2526. #endif // SOFTWARE_MODE_MASTER
  2527. #ifdef GPIO_I2C
  2528. case IOC_GPIOI2C_MASTER_WRITE:
  2529. {
  2530. I2C_INFO I2cInfo;
  2531. UINT8 *pData;
  2532. if(copy_from_user(&I2cInfo, (void __user *)arg, sizeof(I2C_INFO)))
  2533. {
  2534. DbgFunPrint("copy_from_user fail");
  2535. retval = -EFAULT;
  2536. break;
  2537. }
  2538. pData = drv_kmalloc(I2cInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
  2539. if(!pData)
  2540. {
  2541. DbgFunPrint("No resource");
  2542. retval = -ENOMEM;
  2543. break;
  2544. }
  2545. if(copy_from_user(pData, (void __user *)I2cInfo.Data, I2cInfo.DataLength))
  2546. {
  2547. DbgFunPrint("copy_from_user fail");
  2548. retval = -EFAULT;
  2549. drv_kfree(pData, MODULEID_I2C);
  2550. break;
  2551. }
  2552. DRV_GPIOI2C_WriteFun(I2cInfo.MasterIndex
  2553. ,I2cInfo.SlaveAddress // DeviceID
  2554. ,I2cInfo.BaseAddrType
  2555. ,I2cInfo.BaseAddress // Offset
  2556. ,pData, I2cInfo.DataLength
  2557. ,I2cInfo.Speed
  2558. ,&Status);
  2559. // copy_to_user((void __user *)&I2cInfo.Status,Status,sizeof(Status));
  2560. drv_kfree(pData, MODULEID_I2C);
  2561. if(Status)
  2562. {
  2563. DbgFunPrint("Status Error:%d",Status);
  2564. retval = -EFAULT;
  2565. break;
  2566. }
  2567. break;
  2568. }
  2569. case IOC_GPIOI2C_MASTER_READ:
  2570. {
  2571. I2C_INFO I2cInfo;
  2572. UINT8 *pData;
  2573. if(copy_from_user(&I2cInfo, (void __user *)arg, sizeof(I2C_INFO)))
  2574. {
  2575. DbgFunPrint("copy_from_user fail");
  2576. retval = -EFAULT;
  2577. break;
  2578. }
  2579. pData = drv_kmalloc(I2cInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
  2580. if(!pData)
  2581. {
  2582. DbgFunPrint("No resource");
  2583. retval = -ENOMEM;
  2584. break;
  2585. }
  2586. // memset(pData,0,I2cInfo.DataLength);
  2587. DRV_GPIOI2C_ReadFun(I2cInfo.MasterIndex
  2588. ,I2cInfo.SlaveAddress // DeviceID
  2589. ,I2cInfo.BaseAddrType
  2590. ,I2cInfo.BaseAddress // Offset
  2591. ,pData, I2cInfo.DataLength
  2592. ,I2cInfo.Speed
  2593. ,&Status);
  2594. // copy_to_user((void __user *)&I2cInfo.Status,Status,sizeof(Status));
  2595. if(Status)
  2596. {
  2597. DbgFunPrint("Status Error:%d",Status);
  2598. retval = -EFAULT;
  2599. drv_kfree(pData, MODULEID_I2C);
  2600. break;
  2601. }
  2602. if(copy_to_user((void __user *)I2cInfo.Data,pData,I2cInfo.DataLength))
  2603. {
  2604. DbgFunPrint("__copy_to_user fail");
  2605. retval = -EFAULT;
  2606. drv_kfree(pData, MODULEID_I2C);
  2607. break;
  2608. }
  2609. drv_kfree(pData, MODULEID_I2C);
  2610. break;
  2611. }
  2612. case IOC_I2C_MASTER_WRITE:
  2613. {
  2614. I2C_INFO I2cInfo;
  2615. UINT8 *pData;
  2616. if(copy_from_user(&I2cInfo, (void __user *)arg, sizeof(I2C_INFO)))
  2617. {
  2618. DbgFunPrint("copy_from_user fail");
  2619. retval = -EFAULT;
  2620. break;
  2621. }
  2622. pData = drv_kmalloc(I2cInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
  2623. if(!pData)
  2624. {
  2625. DbgFunPrint("No resource");
  2626. retval = -ENOMEM;
  2627. break;
  2628. }
  2629. if(copy_from_user(pData, (void __user *)I2cInfo.Data, I2cInfo.DataLength))
  2630. {
  2631. DbgFunPrint("copy_from_user fail");
  2632. retval = -EFAULT;
  2633. drv_kfree(pData, MODULEID_I2C);
  2634. break;
  2635. }
  2636. DRV_I2C_WriteFun(I2cInfo.MasterIndex
  2637. ,I2cInfo.SlaveAddress // DeviceID
  2638. ,I2cInfo.BaseAddrType
  2639. ,I2cInfo.BaseAddress // Offset
  2640. ,pData, I2cInfo.DataLength
  2641. ,I2cInfo.Speed
  2642. ,&Status);
  2643. // copy_to_user((void __user *)&I2cInfo.Status,Status,sizeof(Status));
  2644. drv_kfree(pData, MODULEID_I2C);
  2645. if(Status)
  2646. {
  2647. DbgFunPrint("Status Error:%d",Status);
  2648. retval = -EFAULT;
  2649. break;
  2650. }
  2651. break;
  2652. }
  2653. #endif
  2654. case IOC_I2C_MASTER_READ:
  2655. {
  2656. I2C_INFO I2cInfo;
  2657. UINT8 *pData;
  2658. if(copy_from_user(&I2cInfo, (void __user *)arg, sizeof(I2C_INFO)))
  2659. {
  2660. DbgFunPrint("copy_from_user fail");
  2661. retval = -EFAULT;
  2662. break;
  2663. }
  2664. pData = drv_kmalloc(I2cInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
  2665. if(!pData)
  2666. {
  2667. DbgFunPrint("No resource");
  2668. retval = -ENOMEM;
  2669. break;
  2670. }
  2671. // memset(pData,0,I2cInfo.DataLength);
  2672. DRV_I2C_ReadFun(I2cInfo.MasterIndex
  2673. ,I2cInfo.SlaveAddress // DeviceID
  2674. ,I2cInfo.BaseAddrType
  2675. ,I2cInfo.BaseAddress // Offset
  2676. ,pData, I2cInfo.DataLength
  2677. ,I2cInfo.Speed
  2678. ,&Status);
  2679. // copy_to_user((void __user *)&I2cInfo.Status,Status,sizeof(Status));
  2680. if(Status)
  2681. {
  2682. DbgFunPrint("Status Error:%d",Status);
  2683. retval = -EFAULT;
  2684. drv_kfree(pData, MODULEID_I2C);
  2685. break;
  2686. }
  2687. if(copy_to_user((void __user *)I2cInfo.Data,pData,I2cInfo.DataLength))
  2688. {
  2689. DbgFunPrint("__copy_to_user fail");
  2690. retval = -EFAULT;
  2691. drv_kfree(pData, MODULEID_I2C);
  2692. break;
  2693. }
  2694. drv_kfree(pData, MODULEID_I2C);
  2695. break;
  2696. }
  2697. case IOC_I2C_DUMP_MEM:
  2698. {
  2699. I2C_DUMP I2cDump;
  2700. UINT32 *Addr, Length,i;
  2701. if(copy_from_user(&I2cDump, (void __user *)arg, sizeof(I2C_DUMP)))
  2702. {
  2703. DbgFunPrint("copy_from_user fail");
  2704. retval = -EFAULT;
  2705. break;
  2706. }
  2707. Addr = (UINT32 *)((UINT32)I2cDump.Addr & ~0x3); //4 bytes alignment
  2708. Length = I2cDump.Length & ~0x3; //4 bytes alignment
  2709. // DebugPrint("dump:Addr=0x%08x Length=%u",(UINT32)Addr,Length);
  2710. if(Length==0)
  2711. break;
  2712. printk("????????????????????\n");
  2713. for(i=0;i<Length;)
  2714. {
  2715. if((i&0xF)==0)
  2716. printk("0x%08X: ",(UINT32)Addr);
  2717. printk("%08X ",MmioReadFun(Addr));
  2718. Addr++;
  2719. i += 4;
  2720. if((i&0xF)==0)
  2721. printk("\n");
  2722. }
  2723. if((i&0xF)!=0)
  2724. printk("\n");
  2725. printk("????????????????????\n");
  2726. break;
  2727. }
  2728. //#endif
  2729. default:
  2730. retval = -ENOTTY;
  2731. break;
  2732. }
  2733. return retval;
  2734. }
  2735. static int I2cOpenFun(struct inode *inode, struct file *pFile)
  2736. {
  2737. // DebugPrint("I2cOpenFun");
  2738. pFile->private_data = (void*) &I2cDev;
  2739. return 0;
  2740. }
  2741. static int I2cCloseFun(struct inode *inode, struct file *pFile)
  2742. {
  2743. // DebugPrint("I2cCloseFun");
  2744. return 0;
  2745. }
  2746. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,8)
  2747. static struct file_operations I2cFops = {
  2748. .owner = THIS_MODULE,
  2749. // .read = NsmReadFun,
  2750. // .write = NsmWriteFun,
  2751. .unlocked_ioctl = I2cIoctlFun,
  2752. .open = I2cOpenFun,
  2753. .release = I2cCloseFun,
  2754. };
  2755. #else
  2756. static struct file_operations I2cFops = {
  2757. .owner = THIS_MODULE,
  2758. // .read = NsmReadFun,
  2759. // .write = NsmWriteFun,
  2760. .ioctl = I2cIoctlFun,
  2761. .open = I2cOpenFun,
  2762. .release = I2cCloseFun,
  2763. };
  2764. #endif
  2765. #if 1
  2766. //330 did ot supprt suspend/resume
  2767. #else
  2768. INT32 I2CSuspend(pm_message_t state)
  2769. {
  2770. printk(KERN_EMERG "[I2C]I2CSuspend!!\n");
  2771. UINT32 RegValue;
  2772. bSuspend = 1;
  2773. RegValue &= ~MX_CONTROL0_INT_EN;
  2774. MmioWriteFun(M0_CONTROL0,RegValue);
  2775. MmioWriteFun(M1_CONTROL0,RegValue);
  2776. complete(&I2cDev.Comp[0]);
  2777. complete(&I2cDev.Comp[1]);
  2778. //cdev_del(&I2cDev.cdev);
  2779. //destroy_workqueue(I2cDev.i2cWorkQueue);
  2780. cancel_work_sync(&I2cDev.QueueWork);
  2781. //flush_delayed_work(I2cDev.i2cWorkQueue);
  2782. MmioWriteFun(M0_CONTROL0,0x00000000);
  2783. MmioWriteFun(M1_CONTROL0,0x00000000);
  2784. MmioWriteFun(SW_MASTER0,0x000C000C); // disable software mode
  2785. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
  2786. //del_timer_sync(&I2cDev.ResetTimer[0]);
  2787. //free_irq(MASTER_IRQ_FINAL,NULL);
  2788. #endif
  2789. return 1;
  2790. }
  2791. INT32 I2CResume(void)
  2792. {
  2793. printk(KERN_EMERG "[I2C]I2CResume!!\n");
  2794. INT32 i,result=0;
  2795. INT32 ret;
  2796. dev_t devno;
  2797. // UINT8 bData;
  2798. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
  2799. DebugPrint("I2cInit hardware mode");
  2800. #elif (I2C_MODE==I2C_SOFTWARE_MODE_MASTER)
  2801. DebugPrint("I2cInit software master mode");
  2802. #endif
  2803. memset(&I2cDev,0,sizeof(I2C_DEV));
  2804. for(i=0;i<MASTER_NUMBER;i++)
  2805. {
  2806. sema_init(&I2cDev.I2cMutex[i], 1);
  2807. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
  2808. init_completion(&I2cDev.Comp[i]);
  2809. //init_timer(&I2cDev.ResetTimer[i]);
  2810. //I2cDev.ResetTimer[i].data = (UINT32)&I2cDev;
  2811. #endif
  2812. }
  2813. sema_init(&I2cDev.GPIOI2cMutex, 1);
  2814. sema_init(&I2cDev.GPIOXI2cMutex, 1);
  2815. INIT_LIST_HEAD(&I2cDev.pQueueWriteList);
  2816. /*
  2817. I2cDev.i2cWorkQueue = create_singlethread_workqueue("I2C");
  2818. if(!I2cDev.i2cWorkQueue)
  2819. {
  2820. DebugPrint("Cannot create_singlethread_workqueue");
  2821. return -ENOMEM;
  2822. }
  2823. INIT_WORK(&I2cDev.QueueWork,RunQueueFun);
  2824. */
  2825. for(i=0;i<I2C_SPIN_LOCK_MAX;i++)
  2826. {
  2827. spin_lock_init(&I2cDev.Spinlock[i]);
  2828. }
  2829. MmioWriteFun(SW_MASTER0,0x000C000C); // disable software mode
  2830. if((readb((void *)SW_MASTER0)&0x30)!=0x30)
  2831. {
  2832. DebugPrint("Error: SDA|SCL=%02x !",readb((void *)SW_MASTER0));
  2833. }
  2834. /*
  2835. devno = MKDEV(I2C_DEV_MAJOR, 0); // I2C_DEV_MAJOR=91
  2836. cdev_init(&I2cDev.cdev, &I2cFops);
  2837. I2cDev.cdev.owner = THIS_MODULE;
  2838. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
  2839. set_vi_handler(MASTER_IRQ_FINAL,i2c_dispatch);
  2840. result = request_irq(MASTER_IRQ_FINAL,MasterI2cInterruptFun,0,"master_i2c",NULL);
  2841. if(result)
  2842. {
  2843. DebugPrint("request_irq fault");
  2844. return result;
  2845. }
  2846. #endif
  2847. */
  2848. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
  2849. #if ONE_BYTE_INTERRUPT
  2850. MmioWriteFun(M0_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN|MX_CONTROL0_ONE_BYTE_INT);
  2851. MmioWriteFun(M1_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN|MX_CONTROL0_ONE_BYTE_INT);
  2852. #else
  2853. MmioWriteFun(M0_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN);
  2854. MmioWriteFun(M1_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN);
  2855. #endif
  2856. MasterAsyncResetFun(0,I2C_SPEED_100K); // Asynchronous reset
  2857. MasterAsyncResetFun(1,I2C_SPEED_100K); // Asynchronous reset
  2858. DebugPrint("Info[0]=0x%08x, Info[1]=0x%08x",(UINT32)&I2cDev.IntInfo[0],(UINT32)&I2cDev.IntInfo[1]);
  2859. #endif
  2860. bSuspend = 0;
  2861. /*
  2862. result = cdev_add(&I2cDev.cdev, devno, 1);
  2863. if(result)
  2864. {
  2865. DebugPrint("cdev_add fault");
  2866. return result;
  2867. }
  2868. */
  2869. return result;
  2870. }
  2871. #endif
  2872. dev_t devno;
  2873. INT32 __init I2cInit(void)
  2874. {
  2875. INT32 i,result=0;
  2876. // dev_t devno;
  2877. // UINT8 bData;
  2878. #if CONFIG_CHIPID == 0x330
  2879. UINT32 value;
  2880. #endif
  2881. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
  2882. DebugPrint("I2cInit hardware mode");
  2883. #elif (I2C_MODE==I2C_SOFTWARE_MODE_MASTER)
  2884. DebugPrint("I2cInit software master mode");
  2885. #endif
  2886. /* // Hardware reset
  2887. bData = mReadByteFun(0xBE00000C);
  2888. bData |= 0x04;
  2889. mWriteByteFun(0xBE00000C,bData);
  2890. udelay(1);
  2891. bData &= ~0x04;
  2892. mWriteByteFun(0xBE00000C,bData);
  2893. */
  2894. memset(&I2cDev,0,sizeof(I2C_DEV));
  2895. for(i=0;i<MASTER_NUMBER;i++)
  2896. {
  2897. sema_init(&I2cDev.I2cMutex[i], 1);
  2898. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
  2899. init_completion(&I2cDev.Comp[i]);
  2900. //init_timer(&I2cDev.ResetTimer[i]);
  2901. //I2cDev.ResetTimer[i].data = (UINT32)&I2cDev;
  2902. #endif
  2903. }
  2904. sema_init(&I2cDev.GPIOI2cMutex, 1);
  2905. sema_init(&I2cDev.GPIOXI2cMutex, 1);
  2906. /*#if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
  2907. I2cDev.ResetTimer[0].function = i2cM0TimeOutFun;
  2908. I2cDev.ResetTimer[1].function = i2cM1TimeOutFun;
  2909. #endif*/
  2910. INIT_LIST_HEAD(&I2cDev.pQueueWriteList);
  2911. INIT_LIST_HEAD(&CmdList);
  2912. I2cDev.i2cWorkQueue = create_singlethread_workqueue("I2C");
  2913. if(!I2cDev.i2cWorkQueue)
  2914. {
  2915. DebugPrint("Cannot create_singlethread_workqueue");
  2916. return -ENOMEM;
  2917. }
  2918. INIT_WORK(&I2cDev.QueueWork,RunQueueFun);
  2919. for(i=0;i<I2C_SPIN_LOCK_MAX;i++)
  2920. {
  2921. spin_lock_init(&I2cDev.Spinlock[i]);
  2922. }
  2923. MmioWriteFun(SW_MASTER0,0x000C000C); // disable software mode
  2924. if((readb((void *)SW_MASTER0)&0x30)!=0x30)
  2925. {
  2926. printk("%s:%d SW_MASTER0 : SDA|SCL=%02x !",__FUNCTION__,__LINE__,readb((void *)SW_MASTER0));
  2927. }
  2928. devno = MKDEV(I2C_DEV_MAJOR, 0); // I2C_DEV_MAJOR=91
  2929. result = register_chrdev_region(devno, 1, "I2C"); //20110807 lwhite add
  2930. if(result)
  2931. {
  2932. DebugPrint("register_chrdev_region fault");
  2933. return result;
  2934. }
  2935. cdev_init(&I2cDev.cdev, &I2cFops);
  2936. I2cDev.cdev.owner = THIS_MODULE;
  2937. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
  2938. set_vi_handler(MASTER_A_IRQ_FINAL,i2c_dispatch_A);
  2939. result = request_irq(MASTER_A_IRQ_FINAL,MasterAI2cInterruptFun,/*SA_INTERRUPT*/0,"master_i2c",NULL);
  2940. if(result)
  2941. {
  2942. DebugPrint("request_irq fault");
  2943. return result;
  2944. }
  2945. set_vi_handler(MASTER_B_IRQ_FINAL,i2c_dispatch_B);
  2946. result = request_irq(MASTER_B_IRQ_FINAL,MasterBI2cInterruptFun,/*SA_INTERRUPT*/0,"master_i2c",NULL);
  2947. if(result)
  2948. {
  2949. DebugPrint("request_irq fault");
  2950. return result;
  2951. }
  2952. #endif
  2953. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
  2954. #if ONE_BYTE_INTERRUPT
  2955. MmioWriteFun(M0_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN|MX_CONTROL0_ONE_BYTE_INT|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
  2956. MmioWriteFun(M1_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN|MX_CONTROL0_ONE_BYTE_INT|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
  2957. #else
  2958. MmioWriteFun(M0_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
  2959. MmioWriteFun(M1_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
  2960. #endif
  2961. MasterAsyncResetFun(0,I2C_SPEED_100K); // Asynchronous reset
  2962. MasterAsyncResetFun(1,I2C_SPEED_100K); // Asynchronous reset
  2963. DebugPrint("Info[0]=0x%08x, Info[1]=0x%08x",(UINT32)&I2cDev.IntInfo[0],(UINT32)&I2cDev.IntInfo[1]);
  2964. #endif
  2965. result = cdev_add(&I2cDev.cdev, devno, 1);
  2966. if(result)
  2967. {
  2968. DebugPrint("cdev_add fault");
  2969. return result;
  2970. }
  2971. #if 1
  2972. //330 did ot supprt suspend/resume
  2973. #else
  2974. kmf_register_platformfunc(MODULEID_I2C, I2CSuspend, I2CResume);
  2975. #endif
  2976. #if CONFIG_CHIPID == 0x330
  2977. //Enable the second I2C master
  2978. value = readl((UINT32*)0xbe0f0604); //open gpio 21,22
  2979. value = (value & 0xffffc3ff) | 0x00002800;
  2980. writel(value, (UINT32*)0xbe0f0604);
  2981. value = readl((UINT32*)0xbe0f0620);
  2982. // value = (value & 0xff9fffff) | 0x00600000;//Enable internal pull-up
  2983. value = (value & 0xff9fffff) ;//Disable internal pull-up
  2984. writel(value, (UINT32*)0xbe0f0620);
  2985. #endif
  2986. if((readb((void *)SW_MASTER1)&0x30)!=0x30)
  2987. {
  2988. printk("%s:%d SW_MASTER1 : SDA|SCL=%02x !",__FUNCTION__,__LINE__,readb((void *)SW_MASTER1));
  2989. }
  2990. return result;
  2991. }
  2992. void __exit I2cExit(void)
  2993. {
  2994. // struct delayed_work delaywork;
  2995. //struct work_struct *work;
  2996. //struct work_struct QueueWork;
  2997. //PI2C_DEV pI2cDev = container_of(work, I2C_DEV, QueueWork);
  2998. //PI2C_DEV pI2cDev = &I2cDev;
  2999. DebugPrint("I2cExit");
  3000. cdev_del(&I2cDev.cdev);
  3001. unregister_chrdev_region(devno, 1); //20110807 lwhite add
  3002. /*
  3003. cancel_delayed_work(&pI2cDev->QueueWork);
  3004. flush_workqueue(pI2cDev->i2cWorkQueue);
  3005. destroy_workqueue(pI2cDev->i2cWorkQueue);
  3006. */
  3007. // struct work_struct *testWork;
  3008. // cancel_delayed_work(&testWork);
  3009. // INIT_DELAYED_WORK(&delaywork,RunQueueFun);
  3010. // cancel_delayed_work(&delaywork);
  3011. // flush_workqueue(I2cDev.i2cWorkQueue);
  3012. destroy_workqueue(I2cDev.i2cWorkQueue);
  3013. MmioWriteFun(M0_CONTROL0,0x00000000);
  3014. MmioWriteFun(M1_CONTROL0,0x00000000);
  3015. MmioWriteFun(SW_MASTER0,0x000C000C); // disable software mode
  3016. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
  3017. /*INT32 i;
  3018. for(i=0;i<MASTER_NUMBER;i++)
  3019. {
  3020. del_timer_sync(&I2cDev.ResetTimer[i]);
  3021. }*/
  3022. free_irq(MASTER_A_IRQ_FINAL,NULL);
  3023. free_irq(MASTER_B_IRQ_FINAL,NULL);
  3024. #endif
  3025. }
  3026. #ifndef INIT_BY_KMF
  3027. module_init (I2cInit);
  3028. module_exit (I2cExit);
  3029. MODULE_LICENSE("GPL");
  3030. MODULE_AUTHOR("Cheng-Hung Wang");
  3031. #endif
  3032. #if SOFTWARE_MODE_MASTER
  3033. //EXPORT_SYMBOL(SwI2CWriteBasicFun);
  3034. //EXPORT_SYMBOL(SwI2CReadBasicFun);
  3035. //EXPORT_SYMBOL(SwI2CWriteMultiBytesFun);
  3036. EXPORT_SYMBOL(DRV_SwI2C_ReadFun);
  3037. //EXPORT_SYMBOL(SwI2CReadMultiBytesTempFun); //20081110 lwhite test
  3038. #endif
  3039. #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
  3040. EXPORT_SYMBOL(DRV_I2C_ReadFun);
  3041. EXPORT_SYMBOL(DRV_I2C_WriteFun);
  3042. EXPORT_SYMBOL(DRV_I2C_M0BasicReadFun);
  3043. EXPORT_SYMBOL(DRV_I2C_M1BasicReadFun);
  3044. EXPORT_SYMBOL(DRV_I2C_M0BasicWriteFun);
  3045. EXPORT_SYMBOL(DRV_I2C_M1BasicWriteFun);
  3046. EXPORT_SYMBOL(DRV_I2C_QueueCmdFun);
  3047. EXPORT_SYMBOL(DRV_I2C_RunCmdFun);
  3048. #endif