12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386 |
- #include <linux/version.h>
- #include <asm/mipsregs.h>
- #include <drv_devices.h>
- #include <drv_platform.h>
- #include <drv_i2c.h>
- #include <linux/slab.h> //20100915 lwhite add this for porting driver to Kernel-2.6.35 (for drv_kmalloc & drv_vfree)
- #include "drv_i2c_main.h"
- I2C_DEV I2cDev;
- struct list_head CmdList;
- UINT32 M0BusyCounter = 0;
- UINT32 M1BusyCounter = 0;
- UINT8 bSuspend = 0;
- UINT16 g_ucHangNum = 0;
- UINT16 g_BusyLoop = 100;
- #define I2C_STATUS_SYSTEM_TIMEOUT 6
- #define GPIO_I2C
- #if 1
- //330 did ot supprt suspend/resume
- #else
- INT32 I2CResume(void);
- INT32 I2CSuspend(pm_message_t state);
- #endif
- INT32 DRV_I2C_QueueCmdFun(UINT8 Mx,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed, INT8 bRead);
- INT32 DRV_I2C_RunCmdFun(UINT8 Mx,UINT8 *pData);
- void RunQueueFun(struct work_struct *work);
- void MxI2CRestart(UINT8 Mx,UINT32 lag);
- void MxI2CStart(UINT8 Mx,UINT32 lag);
- void MxI2CStop(UINT8 Mx,UINT32 lag);
- void MxI2CWriteByte(UINT8 Mx,UINT8 Value,UINT32 lag);
- UINT32 SwI2CReadBasicFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 BaseAddress,UINT8 Length,UINT8 Speed,UINT8 *pStatus);
- void SwI2CWriteBasicFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 BaseAddress,UINT32 Data,UINT8 Length,UINT8 Speed,UINT8 *pStatus);
- void I2CRead(UINT8 MasterIndex,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed,UINT8 *pStatus);
- void I2CWrite(UINT8 MasterIndex,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed,UINT8 *pStatus);
- static INT32 QueueI2CFun(UINT8 Mx,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed)
- {
- unsigned long flags;
- PQUEUE_I2C pQueue;
- pQueue = drv_kmalloc(sizeof(QUEUE_I2C)+Length-1,GFP_ATOMIC, MODULEID_I2C); // Cannot sleep
- if(!pQueue)
- {
- DebugPrint("Cannot allocate QUEUE_I2C");
- return -ENOMEM;
- }
- // INIT_LIST_HEAD(&pQueue->list);
- pQueue->Mx = Mx;
- pQueue->DeviceID = DeviceID;
- pQueue->AddrType = AddrType;
- pQueue->Speed = Speed;
- pQueue->Address = Address;
- pQueue->Length = Length;
- memcpy(pQueue->Data,pData,Length);
- spin_lock_irqsave(&I2cDev.Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
- list_add_tail(&pQueue->list,&I2cDev.pQueueWriteList);
- spin_unlock_irqrestore(&I2cDev.Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
- queue_work(I2cDev.i2cWorkQueue,&I2cDev.QueueWork);
- DebugPrint("WARNING! Queue Mx=%d, ID=%02x, Type=%d, Addr=%08x, Len=%d, Speed=%d"
- ,Mx,DeviceID,AddrType,Address,Length,Speed);
- return 0;
- }
- /*
- void RunQueueFun(void *Context)
- {
- struct list_head QueueList,*cursor,*next;
- UINT32 flags;
- PQUEUE_I2C pQueue;
- UINT8 Status;
- INIT_LIST_HEAD(&QueueList);
- spin_lock_irqsave(&I2cDev.Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
- list_splice_init(&I2cDev.pQueueWriteList,&QueueList);
- spin_unlock_irqrestore(&I2cDev.Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
- list_for_each_safe(cursor,next,&QueueList)
- {
- list_del(cursor);
- pQueue = container_of(cursor,QUEUE_I2C,list);
- Status = I2C_STATUS_SUCCESS;
- // DebugPrint("RunQueue Mx=%d, ID=%02x, Type=%d, Addr=%08x, Len=%d, Speed=%d"
- // ,pQueue->Mx,pQueue->DeviceID,pQueue->AddrType
- // ,pQueue->Address,pQueue->Length,pQueue->Speed);
- DRV_I2C_WriteFun(pQueue->Mx
- ,pQueue->DeviceID
- ,pQueue->AddrType
- ,pQueue->Address
- ,pQueue->Data,pQueue->Length
- ,pQueue->Speed,&Status);
- drv_kfree(pQueue, MODULEID_I2C);
- }
- }
- */
- //20100209 lwhite modified for new kernel
- void RunQueueFun(struct work_struct *work)
- {
- PI2C_DEV pI2cDev = container_of(work, I2C_DEV, QueueWork);
- struct list_head QueueList,*cursor = NULL,*next = NULL;
- unsigned long flags;
- UINT8 Status;
- INIT_LIST_HEAD(&QueueList);
- spin_lock_irqsave(&pI2cDev->Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
- list_splice_init(&pI2cDev->pQueueWriteList,&QueueList);
- spin_unlock_irqrestore(&pI2cDev->Spinlock[I2C_SPIN_LOCK_QUEUE],flags);
- list_for_each_safe(cursor,next,&QueueList)
- {
- PQUEUE_I2C pQueue;
- list_del(cursor);
- pQueue = container_of(cursor,QUEUE_I2C,list);
- Status = I2C_STATUS_SUCCESS;
- // DebugPrint("RunQueue Mx=%d, ID=%02x, Type=%d, Addr=%08x, Len=%d, Speed=%d"
- // ,pQueue->Mx,pQueue->DeviceID,pQueue->AddrType
- // ,pQueue->Address,pQueue->Length,pQueue->Speed);
- DRV_I2C_WriteFun(pQueue->Mx
- ,pQueue->DeviceID
- ,pQueue->AddrType
- ,pQueue->Address
- ,pQueue->Data,pQueue->Length
- ,pQueue->Speed,&Status);
- drv_kfree(pQueue, MODULEID_I2C);
- }
- }
- #if SOFTWARE_MODE_MASTER
- #define newdelay(time2) {\
- UINT32 TargetA,curA,refA;\
- MmioReadFun(M0_CONTROL0);\
- refA = read_c0_count();\
- TargetA = (time2)*100 + refA;\
- if(TargetA<refA)\
- {\
- do\
- {\
- udelay(1);\
- curA = read_c0_count();\
- }while(curA>refA||(curA<refA && curA<TargetA));\
- }\
- else\
- {\
- do\
- {\
- udelay(1);\
- curA = read_c0_count();\
- }while(curA<TargetA && curA>refA);\
- }\
- }
- #define WaitClk(time,hl) {\
- UINT32 limit;\
- if(hl==1)\
- {\
- for(limit=0;limit<12 && (SW_MASTER_SCL_IN&MmioReadWordFun(Control))==0;limit++)\
- {\
- udelay(1);\
- }\
- }\
- else\
- {\
- for(limit=0;limit<12 && (SW_MASTER_SCL_IN&MmioReadWordFun(Control))!=0;limit++)\
- {\
- udelay(1);\
- }\
- }\
- if(time>limit)\
- newdelay(time-limit);\
- }
- void MxI2CStart(UINT8 Mx,UINT32 lag)
- {
- PVOID Control;
- UINT16 Data;
- if(Mx==0)
- {
- Control = (PVOID)SW_MASTER0;
- }
- else
- {
- Control = (PVOID)SW_MASTER1;
- }
- Data = MmioReadWordFun(Control);
- Data |= SW_MASTER_ENABLE | SW_MASTER_SCL_OUT_OEN | SW_MASTER_SDA_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:1, Clk:1
- newdelay(2);
- Data &= ~SW_MASTER_SDA_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:0, Clk:1
- newdelay(lag);
- Data &= ~SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:0, Clk:0
- WaitClk(lag,0);
- I2cDev.Status[Mx] = I2C_STATUS_SUCCESS;
- }
- void MxI2CStop(UINT8 Mx,UINT32 lag)
- {
- PVOID Control;
- UINT16 Data;
- if(Mx==0)
- {
- Control = (PVOID)SW_MASTER0;
- }
- else
- {
- Control = (PVOID)SW_MASTER1;
- }
- Data = MmioReadWordFun(Control);
- Data &= ~(SW_MASTER_SDA_OUT_OEN | SW_MASTER_SCL_OUT_OEN);
- MmioWriteWordFun(Control,Data); // Data:0, Clk:0
- newdelay(lag);
- Data |= SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:0, Clk:1
- WaitClk(lag,1);
- Data |= SW_MASTER_SDA_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:1, Clk:1
- newdelay(lag);
- Data &= ~SW_MASTER_ENABLE;
- MmioWriteWordFun(Control,Data);
- newdelay(2);
- }
- void MxI2CRestart(UINT8 Mx,UINT32 lag)
- {
- PVOID Control;
- UINT16 Data;
- if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
- return;
- if(Mx==0)
- {
- Control = (PVOID)SW_MASTER0;
- }
- else
- {
- Control = (PVOID)SW_MASTER1;
- }
- Data = MmioReadWordFun(Control);
- Data &= ~SW_MASTER_SCL_OUT_OEN;
- Data |= SW_MASTER_SDA_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:1, Clk:0
- newdelay(lag);
- Data |= SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:1, Clk:1
- WaitClk(lag,1);
- Data &= ~SW_MASTER_SDA_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:0, Clk:1
- newdelay(lag);
- Data &= ~SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:0, Clk:0
- WaitClk(lag,0);
- }
- void MxI2CWriteByte(UINT8 Mx,UINT8 Value,UINT32 lag)
- {
- PVOID Control;
- UINT16 Data,i;
- if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
- return;
- if(Mx==0)
- {
- Control = (PVOID)SW_MASTER0;
- }
- else
- {
- Control = (PVOID)SW_MASTER1;
- }
- Data = MmioReadWordFun(Control);
- /* Data &= ~SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:x, Clk:0
- newdelay(lag);*/
- for(i=0;i<8;i++)
- {
- if(Value & 0x80)
- {
- Data |= SW_MASTER_SDA_OUT_OEN;
- }
- else
- {
- Data &= ~SW_MASTER_SDA_OUT_OEN;
- }
- MmioWriteWordFun(Control,Data); // Data:x, Clk:0
- newdelay(lag);
- Data ^= SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:x, Clk:1
- WaitClk(lag,1);
- Data ^= SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:x, Clk:0
- WaitClk(lag,0);
- Value <<= 1;
- }
- Data |= SW_MASTER_SDA_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:1, Clk:0
- newdelay(lag);
- for(i=0;i<8 && (SW_MASTER_SDA_IN & MmioReadWordFun(Control))!=0;i++)
- udelay(1);
- Data ^= SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:ACK, Clk:1
- WaitClk(lag,1);
- if(SW_MASTER_SDA_IN & MmioReadWordFun(Control))
- {
- I2cDev.Status[Mx] = I2C_STATUS_DATA_ERROR;
- }
- Data ^= SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:1, Clk:0
- WaitClk(lag,0);
- }
- UINT8 MxI2CReadByte(UINT8 Mx,UINT32 lag,UINT8 bMoreByte)
- {
- PVOID Control;
- UINT16 Data,i;
- UINT8 Value=0;
- if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
- return -1;
- if(Mx==0)
- {
- Control = (PVOID)SW_MASTER0;
- }
- else
- {
- Control = (PVOID)SW_MASTER1;
- }
- Data = MmioReadWordFun(Control) | SW_MASTER_SDA_OUT_OEN;
- Data &= ~SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:1, Clk:0
- newdelay(lag);
- for(i=0;i<8;i++)
- {
- Value <<= 1;
- Data ^= SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:1, Clk:1
- WaitClk(lag,1);
- if(SW_MASTER_SDA_IN & MmioReadWordFun(Control))
- {
- Value |= 1;
- }
- Data ^= SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:1, Clk:0
- WaitClk(lag,0);
- }
- if(bMoreByte)
- {
- Data &= ~SW_MASTER_SDA_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:0, Clk:0
- newdelay(lag);
- }
- Data ^= SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:x, Clk:1
- WaitClk(lag,1);
- Data ^= SW_MASTER_SCL_OUT_OEN;
- MmioWriteWordFun(Control,Data); // Data:x, Clk:0
- WaitClk(lag,0);
- return Value;
- }
- #if 0
- UINT32 SwI2CReadBasicFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 BaseAddress,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
- {
- //UINT32 Data,i;
- UINT32 Data;
- UINT8 i;
- UINT32 lag;
- UINT8 *pData;
- if(Length>4)
- {
- *pStatus = I2C_STATUS_DATA_ERROR;
- DbgFunPrint("Invalid Parameter");
- return -1;
- }
- if(in_atomic())
- {
- DbgFunPrint("ID=%02x Addr=%02x,Invalid Process!!",SlaveAddress,BaseAddress);
- *pStatus = I2C_STATUS_DATA_ERROR;
- return -1;
- }
- if(Speed==I2C_SPEED_100K)
- {
- lag = SW_MASTER_100K;
- }
- else
- {
- lag = SW_MASTER_400K;
- }
- Data = 0;
- pData = (UINT8 *)&Data;
- down(&I2cDev.I2cMutex[Mx]);
- MxI2CStart(Mx,lag);
- MxI2CWriteByte(Mx,SlaveAddress,lag);
- MxI2CWriteByte(Mx,BaseAddress,lag);
- MxI2CRestart(Mx,lag);
- MxI2CWriteByte(Mx,SlaveAddress|1,lag);
- for(i=0;i<Length-1;i++)
- {
- pData[i] = MxI2CReadByte(Mx,lag,TRUE);
- }
- pData[i] = MxI2CReadByte(Mx,lag,FALSE);
- MxI2CStop(Mx,lag);
- *pStatus = I2cDev.Status[Mx];
- if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
- {
- DbgFunPrint("Read M%d %02x %02x Fail %d",Mx,SlaveAddress,BaseAddress,Length);
- }
- up(&I2cDev.I2cMutex[Mx]);
- return Data;
- }
- void SwI2CWriteBasicFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 BaseAddress,UINT32 Data,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
- {
- UINT32 lag,i;
- UINT8 *pData;
- if(Length>4)
- {
- *pStatus = I2C_STATUS_DATA_ERROR;
- DbgFunPrint("Invalid Parameter");
- return;
- }
- if(in_atomic())
- {
- if(QueueI2CFun(Mx,SlaveAddress,ADDRESS_TYPE_BYTE,BaseAddress,(UINT8 *)&Data,Length,Speed)<0)
- {
- DbgFunPrint("Invalid Process!!");
- *pStatus = I2C_STATUS_DATA_ERROR;
- }
- else
- {
- *pStatus = I2C_STATUS_SUCCESS;
- }
- return;
- }
- if(Speed==I2C_SPEED_100K)
- {
- lag = SW_MASTER_100K;
- }
- else
- {
- lag = SW_MASTER_400K;
- }
- pData = (UINT8 *)&Data;
- down(&I2cDev.I2cMutex[Mx]);
- MxI2CStart(Mx,lag);
- MxI2CWriteByte(Mx,SlaveAddress,lag);
- MxI2CWriteByte(Mx,BaseAddress,lag);
- for(i=0;i<Length;i++)
- {
- MxI2CWriteByte(Mx,pData[i],lag);
- }
- MxI2CStop(Mx,lag);
- *pStatus = I2cDev.Status[Mx];
- if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
- {
- DbgFunPrint("Write M%d %02x %02x Fail %d 0x%08x",Mx,SlaveAddress,BaseAddress,Length,Data);
- }
- up(&I2cDev.I2cMutex[Mx]);
- }
- #endif
- void DRV_SwI2C_ReadFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 AddrType,UINT32 BaseAddress,UINT8 *pBuffer,UINT16 Length,UINT8 Speed,UINT8 *pStatus)
- {
- UINT16 i;
- UINT32 lag;
- if(in_atomic())
- {
- DbgFunPrint("ID=%02x,Invalid Process!!",SlaveAddress);
- *pStatus = I2C_STATUS_DATA_ERROR;
- return;
- }
- if(Speed==I2C_SPEED_100K)
- {
- lag = SW_MASTER_100K;
- }
- else
- {
- lag = SW_MASTER_400K;
- }
- down(&I2cDev.I2cMutex[Mx]);
- MxI2CStart(Mx,lag);
- if(AddrType==ADDRESS_TYPE_BYTE)
- {
- MxI2CWriteByte(Mx,SlaveAddress,lag);
- MxI2CWriteByte(Mx,(UINT8)BaseAddress,lag);
- MxI2CRestart(Mx,lag);
- MxI2CWriteByte(Mx,SlaveAddress|1,lag);
- }
- else if(AddrType==ADDRESS_TYPE_NONE)
- {
- MxI2CWriteByte(Mx,SlaveAddress|1,lag);
- }
- else if(AddrType==ADDRESS_TYPE_MULTIPLE)
- {
- UINT8 AddrNum;
- AddrNum = (BaseAddress>>24) & 0xFF;
- if(AddrNum>3 || AddrNum==0)
- {
- MxI2CStop(Mx,lag);
- *pStatus = I2C_STATUS_DATA_ERROR;
- up(&I2cDev.I2cMutex[Mx]);
- DbgFunPrint("Invalid AddrNum");
- return;
- }
- MxI2CWriteByte(Mx,SlaveAddress,lag);
- for(i=0;i<AddrNum;i++)
- {
- MxI2CWriteByte(Mx,(UINT8)(BaseAddress>>(8*i)),lag);
- }
- MxI2CRestart(Mx,lag);
- MxI2CWriteByte(Mx,SlaveAddress|1,lag);
- }
- for(i=0;i<Length-1;i++)
- {
- pBuffer[i] = MxI2CReadByte(Mx,lag,TRUE);
- }
- pBuffer[i] = MxI2CReadByte(Mx,lag,FALSE);
- MxI2CStop(Mx,lag);
- *pStatus = I2cDev.Status[Mx];
- if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
- {
- DbgFunPrint("Multi-Read M%d %02x Fail",Mx,SlaveAddress);
- }
- up(&I2cDev.I2cMutex[Mx]);
- }
- #if 0
- void SwI2CReadMultiBytesTempFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 AddrType,UINT32 BaseAddress,UINT8 AddrLength,UINT8 *pBuffer,UINT16 DataLength,UINT8 Speed,UINT8 *pStatus)
- {
- UINT16 i;
- UINT32 lag;
- DebugPrint("SwI2CReadMultiBytesTempFun DeviceId = 0x%02x AddrType = %d BaseAddress = 0x%08x AddrLength = %d DataLength = %d Speed = %d",SlaveAddress,AddrType,BaseAddress,AddrLength,DataLength,Speed);
- if(in_atomic())
- {
- DbgFunPrint("ID=%02x,Invalid Process!!",SlaveAddress);
- *pStatus = I2C_STATUS_DATA_ERROR;
- return;
- }
- if(Speed==I2C_SPEED_100K)
- {
- lag = SW_MASTER_100K;
- }
- else
- {
- lag = SW_MASTER_400K;
- }
- down(&I2cDev.I2cMutex[Mx]);
- MxI2CStart(Mx,lag);
- if(AddrType==ADDRESS_TYPE_BYTE)
- {
- MxI2CWriteByte(Mx,SlaveAddress,lag);
- MxI2CWriteByte(Mx,(UINT8)BaseAddress,lag);
- MxI2CRestart(Mx,lag);
- MxI2CWriteByte(Mx,SlaveAddress|1,lag);
- }
- else if(AddrType==ADDRESS_TYPE_NONE)
- {
- MxI2CWriteByte(Mx,SlaveAddress|1,lag);
- }
- else if(AddrType==ADDRESS_TYPE_MULTIPLE)
- {
- if(AddrLength>4 || AddrLength==0)
- {
- MxI2CStop(Mx,lag);
- *pStatus = I2C_STATUS_DATA_ERROR;
- up(&I2cDev.I2cMutex[Mx]);
- DbgFunPrint("Invalid AddrNum");
- return;
- }
- MxI2CWriteByte(Mx,SlaveAddress,lag);
- if (AddrLength==4)
- {
- for(i=0;i<AddrLength;i++)
- {
- MxI2CWriteByte(Mx,(UINT8)(BaseAddress>>(8*i)),lag);
- }
- }
- else if (AddrLength==2)
- {
- UINT16 Addr = (UINT16)BaseAddress;
- for(i=0;i<AddrLength;i++)
- {
- MxI2CWriteByte(Mx,(UINT8)(Addr>>(8*i)),lag);
- }
- }
- else if (AddrLength==1)
- {
- MxI2CWriteByte(Mx,(UINT8)BaseAddress,lag);
- }
- MxI2CRestart(Mx,lag);
- MxI2CWriteByte(Mx,SlaveAddress|1,lag);
- }
- for(i=0;i<DataLength-1;i++)
- {
- pBuffer[i] = MxI2CReadByte(Mx,lag,TRUE);
- }
- pBuffer[i] = MxI2CReadByte(Mx,lag,FALSE);
- MxI2CStop(Mx,lag);
- *pStatus = I2cDev.Status[Mx];
- if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
- {
- DbgFunPrint("Multi-Read M%d %02x Fail",Mx,SlaveAddress);
- }
- up(&I2cDev.I2cMutex[Mx]);
- }
- void SwI2CWriteMultiBytesFun(UINT8 Mx,UINT8 SlaveAddress,UINT8 AddrType,UINT32 BaseAddress,UINT8 *pBuffer,UINT16 Length,UINT8 Speed,UINT8 *pStatus)
- {
- UINT32 i;
- UINT32 lag;
- if(in_atomic())
- {
- if(QueueI2CFun(Mx,SlaveAddress,AddrType,BaseAddress,pBuffer,Length,Speed)<0)
- {
- DbgFunPrint("Invalid Process!!");
- *pStatus = I2C_STATUS_DATA_ERROR;
- }
- else
- {
- *pStatus = I2C_STATUS_SUCCESS;
- }
- return;
- }
- if(Speed==I2C_SPEED_100K)
- {
- lag = SW_MASTER_100K;
- }
- else
- {
- lag = SW_MASTER_400K;
- }
- down(&I2cDev.I2cMutex[Mx]);
- MxI2CStart(Mx,lag);
- MxI2CWriteByte(Mx,SlaveAddress,lag);
- if(AddrType==ADDRESS_TYPE_BYTE)
- {
- MxI2CWriteByte(Mx,(UINT8)BaseAddress,lag);
- }
- else if(AddrType==ADDRESS_TYPE_MULTIPLE)
- {
- UINT8 AddrNum;
- AddrNum = (BaseAddress>>24) & 0xFF;
- if(AddrNum>3 || AddrNum==0)
- {
- MxI2CStop(Mx,lag);
- *pStatus = I2C_STATUS_DATA_ERROR;
- up(&I2cDev.I2cMutex[Mx]);
- DbgFunPrint("Invalid AddrNum");
- return;
- }
- for(i=0;i<AddrNum;i++)
- {
- MxI2CWriteByte(Mx,(UINT8)(BaseAddress>>(8*i)),lag);
- }
- }
- for(i=0;i<Length;i++)
- {
- MxI2CWriteByte(Mx,pBuffer[i],lag);
- }
- MxI2CStop(Mx,lag);
- *pStatus = I2cDev.Status[Mx];
- if(I2cDev.Status[Mx]!=I2C_STATUS_SUCCESS)
- {
- DbgFunPrint("Multi-Write M%d %02x Fail",Mx,SlaveAddress);
- }
- up(&I2cDev.I2cMutex[Mx]);
- }
- #endif
- #endif
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
- #if INTERRUPT_ENABLE
- static irqreturn_t MasterAI2cInterruptFun(INT32 irq,void *dev_id)
- {
- UINT32 BusStatus,Control0,Remainder,Data;
- UINT8 IntReg,CopyBytes;
- UINT8* pBuffer;
- // DebugPrint("MasterI2cInterruptFun");
- BusStatus = MmioReadFun(M0_BUS_STATUS);
- // Master 0
- Control0 = MmioReadFun(M0_CONTROL0);
- if(Control0&MX_CONTROL0_INT)
- {
- IntReg = (UINT8)(BusStatus & 0xFF);
- // DebugPrint("BusStatus=0x%08x",BusStatus);
- if(IntReg==0x00||IntReg==0x0B||IntReg==0xFF)
- {
- I2cDev.Status[0] = I2C_STATUS_DEVICE_ID_ERROR;
- complete(&I2cDev.Comp[0]);
- //del_timer(&I2cDev.ResetTimer[0]);
- DebugPrint("M0 %08x I2C Error!!! 0x%02x",MmioReadFun(M0_CONTROL1),IntReg);
- }
- else
- {
- I2cDev.InterruptCounter[0]--;
- pBuffer = I2cDev.IntInfo[0].pBuffer;
- Remainder = I2cDev.IntInfo[0].Length - I2cDev.IntInfo[0].Completed;
- #if ONE_BYTE_INTERRUPT
- if( (I2cDev.InterruptCounter[0]==0||IntReg==0x04||IntReg==0x08) && pBuffer!=NULL && Remainder>0 )
- #else
- if( (IntReg==0x09||IntReg==0x0A) && pBuffer!=NULL && Remainder>0 )
- #endif
- {
- if(Remainder>4)
- {
- CopyBytes = 4;
- }
- else
- {
- CopyBytes = Remainder;
- }
- if(I2cDev.IntInfo[0].bRead)
- {
- #if ONE_BYTE_INTERRUPT
- if(IntReg==0x05||IntReg==0x06||IntReg==0x07||IntReg==0x08)
- #else
- if(IntReg==0x0A)
- #endif
- {
- Data = MmioReadFun(M0_DATA1);
- }
- else
- {
- Data = MmioReadFun(M0_DATA0);
- }
- memcpy(&pBuffer[I2cDev.IntInfo[0].Completed],&Data,CopyBytes);
- }
- else
- {
- memcpy(&Data,&pBuffer[I2cDev.IntInfo[0].Completed],CopyBytes);
- #if ONE_BYTE_INTERRUPT
- if(IntReg==0x08)
- #else
- if(IntReg==0x0A)
- #endif
- {
- MmioWriteFun(M0_DATA1,Data);
- }
- else
- {
- MmioWriteFun(M0_DATA0,Data);
- }
- }
- I2cDev.IntInfo[0].Completed += CopyBytes;
- }
- if(I2cDev.InterruptCounter[0]==0)
- {
- complete(&I2cDev.Comp[0]);
- //del_timer(&I2cDev.ResetTimer[0]);
- }
- }
- MmioWriteFun(M0_CONTROL0,(Control0&0xF0FFFFFF)|MX_CONTROL0_INT_CLEAR);
- }
- return IRQ_HANDLED;
- }
- static irqreturn_t MasterBI2cInterruptFun(INT32 irq,void *dev_id)
- {
- UINT32 BusStatus,Control0,Remainder,Data;
- UINT8 IntReg,CopyBytes;
- UINT8* pBuffer;
- BusStatus = MmioReadFun(M1_BUS_STATUS);
- // Master 1
- Control0 = MmioReadFun(M1_CONTROL0);
- if(Control0&MX_CONTROL0_INT)
- {
- IntReg = (UINT8)(BusStatus& 0xFF);
- // DebugPrint("BusStatus=0x%08x",BusStatus);
- if(IntReg==0x00||IntReg==0x0B||IntReg==0xFF)
- {
- I2cDev.Status[1] = I2C_STATUS_DEVICE_ID_ERROR;
- complete(&I2cDev.Comp[1]);
- DebugPrint("M1 %08x I2C Error!!! 0x%02x",MmioReadFun(M1_CONTROL1),IntReg);
- }
- else
- {
- I2cDev.InterruptCounter[1]--;
- pBuffer = I2cDev.IntInfo[1].pBuffer;
- Remainder = I2cDev.IntInfo[1].Length - I2cDev.IntInfo[1].Completed;
- #if ONE_BYTE_INTERRUPT
- if( (I2cDev.InterruptCounter[0]==0||IntReg==0x04||IntReg==0x08) && pBuffer!=NULL && Remainder>0 )
- #else
- if( (IntReg==0x09||IntReg==0x0A) && pBuffer!=NULL && Remainder>0 )
- #endif
- {
- if(Remainder>4)
- {
- CopyBytes = 4;
- }
- else
- {
- CopyBytes = Remainder;
- }
- if(I2cDev.IntInfo[1].bRead)
- {
- #if ONE_BYTE_INTERRUPT
- if(IntReg==0x05||IntReg==0x06||IntReg==0x07||IntReg==0x08)
- #else
- if(IntReg==0x0A)
- #endif
- {
- Data = MmioReadFun(M1_DATA1);
- }
- else
- {
- Data = MmioReadFun(M1_DATA0);
- }
- memcpy(&pBuffer[I2cDev.IntInfo[1].Completed],&Data,CopyBytes);
- }
- else
- {
- memcpy(&Data,&pBuffer[I2cDev.IntInfo[1].Completed],CopyBytes);
- #if ONE_BYTE_INTERRUPT
- if(IntReg==0x08)
- #else
- if(IntReg==0x0A)
- #endif
- {
- MmioWriteFun(M1_DATA1,Data);
- }
- else
- {
- MmioWriteFun(M1_DATA0,Data);
- }
- }
- I2cDev.IntInfo[1].Completed += CopyBytes;
- }
- if(I2cDev.InterruptCounter[1]==0)
- {
- complete(&I2cDev.Comp[1]);
- }
- }
- MmioWriteFun(M1_CONTROL0,(Control0&0xF0FFFFFF)|MX_CONTROL0_INT_CLEAR);
- }
- return IRQ_HANDLED;
- }
- //static void i2c_dispatch(struct pt_regs *regs)
- static void i2c_dispatch_A(void)
- {
- do_IRQ(MASTER_A_IRQ_FINAL);
- }
- static void i2c_dispatch_B(void)
- {
- do_IRQ(MASTER_B_IRQ_FINAL);
- }
- /*
- static void i2cM0TimeOutFun(UINT32 data)
- {
- UINT32 reg;
- #ifndef INIT_BY_KMF
- UINT32 i;
- #endif
- DbgFunPrint("Critical Error occurs, InterruptCounter=%d",I2cDev.InterruptCounter[0]);
- #ifndef INIT_BY_KMF
- for(i=0;i<0x100;)
- {
- if((i&0xF)==0)
- printk("0x%08X: ",(UINT32)(M0_CONTROL0+i));
- printk("%08X ",MmioReadFun(M0_CONTROL0+i));
- i += 4;
- if((i&0xF)==0)
- printk("\n");
- }
- if((i&0xF)!=0)
- printk("\n");
- printk("\n");
- #endif
- reg = MmioReadFun(M0_CONTROL0);
- reg &= 0x00FF0000;
- reg |= 0x000000FF;
- MmioWriteFun(M0_CONTROL0,reg);
- I2cDev.Status[0] = I2C_STATUS_HARDWARE_CRASH;
- complete(&I2cDev.Comp[0]);
- }
- static void i2cM1TimeOutFun(UINT32 data)
- {
- UINT32 reg;
- #ifndef INIT_BY_KMF
- UINT32 i;
- #endif
- DebugPrint("i2cM1TimeOutFun, InterruptCounter=%d",I2cDev.InterruptCounter[1]);
- #ifndef INIT_BY_KMF
- for(i=0;i<0x100;)
- {
- if((i&0xF)==0)
- printk("0x%08X: ",(UINT32)(M0_CONTROL0+i));
- printk("%08X ",MmioReadFun(M0_CONTROL0+i));
- i += 4;
- if((i&0xF)==0)
- printk("\n");
- }
- if((i&0xF)!=0)
- printk("\n");
- printk("\n");
- #endif
- reg = MmioReadFun(M1_CONTROL0);
- reg &= 0x00FF0000;
- reg |= 0x000000FF;
- MmioWriteFun(M1_CONTROL0,reg);
- I2cDev.Status[1] = I2C_STATUS_HARDWARE_CRASH;
- complete(&I2cDev.Comp[1]);
- }*/
- #else // else of #if INTERRUPT_ENABLE
- void MasterPollingFun(UINT8 MasterIndex,UINT16 Times)
- {
- INT32 Count;
- UINT32 BusStatus,Control0;
- UINT8 IntReg;
- PVOID Address;
- if(MasterIndex==0)
- {
- Address = (PVOID)M0_CONTROL0;
- }
- else if(MasterIndex==1)
- {
- Address = (PVOID)M1_CONTROL0;
- }
- else
- {
- DbgFunPrint("Invalid Master Index");
- return;
- }
- Count = 0;
- Control0 = MmioReadFun(Address);
- while(Count<Times && I2cDev.InterruptCounter[MasterIndex]>0)
- {
- BusStatus = MmioReadFun(M0_BUS_STATUS);
- IntReg = (UINT8)(BusStatus >> (16*MasterIndex));
- if(IntReg==0x00||IntReg==0x0B||IntReg==0x10||IntReg==0x1B||IntReg==0xFF)
- {
- MmioWriteFun(Address,(Control0&0x00FFFFFF)|MX_CONTROL0_INT_CLEAR);
- DebugPrint("IntReg==0x%02x",IntReg);
- I2cDev.Status[MasterIndex] = I2C_STATUS_DEVICE_ID_ERROR;
- break;
- }
- else
- {
- MmioWriteFun(Address,(Control0&0x00FFFFFF)|MX_CONTROL0_INT_CLEAR);
- Count++;
- I2cDev.InterruptCounter[MasterIndex]--;
- }
- }
- }
- #endif // end of #if INTERRUPT_ENABLE
- static void MasterAsyncResetFun(UINT8 MasterIndex,UINT8 Speed)
- {
- UINT32 RegValue,Scl;
- if(Speed==I2C_SPEED_25K)
- {
- Scl = MX_CONTROL0_SCL_25K;
- }
- else if(Speed==I2C_SPEED_100K)
- {
- Scl = MX_CONTROL0_SCL_100K;
- }
- else if(Speed==I2C_SPEED_200K)
- {
- Scl = MX_CONTROL0_SCL_200K;
- }
- else if(Speed==I2C_SPEED_300K)
- {
- Scl = MX_CONTROL0_SCL_300K;
- }
- else if(Speed==I2C_SPEED_400K)
- {
- Scl = MX_CONTROL0_SCL_400K;
- }
- else
- {
- Scl = MX_CONTROL0_SCL_100K;
- }
- if(MasterIndex)
- {
- RegValue = MmioReadFun(M1_CONTROL0);
- if((RegValue&0xFFFF)!=Scl)
- {
- // DebugPrint("MasterAsyncResetFun");
- RegValue &= 0x00FF0000;
- RegValue |= MX_CONTROL0_ASYNC_RESET|Scl;
- wmb();
- MmioWriteFun(M1_CONTROL0,RegValue|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
- udelay(1);
- RegValue &= ~MX_CONTROL0_ASYNC_RESET;
- wmb();
- MmioWriteFun(M1_CONTROL0,RegValue|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
- udelay(1);
- }
- }
- else
- {
- RegValue = MmioReadFun(M0_CONTROL0);
- if((RegValue&0xFFFF)!=Scl)
- {
- // DebugPrint("MasterAsyncResetFun");
- RegValue &= 0x00FF0000;
- RegValue |= MX_CONTROL0_ASYNC_RESET|Scl;
- wmb();
- MmioWriteFun(M0_CONTROL0,RegValue|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
- udelay(1);
- RegValue &= ~MX_CONTROL0_ASYNC_RESET;
- wmb();
- MmioWriteFun(M0_CONTROL0,RegValue|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
- udelay(1);
- }
- }
- }
- void SW_I2C_DummyRead(UINT8 MasterIndex, UINT8 speed)
- {
- PVOID MxSWI2CBUS;
- UINT8 delayTime = 0, index=0;
- printk("<0>""[I2C][%s]!!\n",__FUNCTION__);
- if ( MasterIndex == 0) {
- MxSWI2CBUS = (PVOID)(M0_CONTROL0+0x100);
- }
- else {
- MxSWI2CBUS = (PVOID)(M1_CONTROL0+0x100);
- }
- switch (speed) {
- case I2C_SPEED_25K:
- delayTime = 20;
- break;
- case I2C_SPEED_100K:
- delayTime = 5;
- break;
- case I2C_SPEED_200K:
- delayTime = 3;
- break;
- case I2C_SPEED_300K:
- case I2C_SPEED_400K:
- delayTime = 2;
- break;
- default:
- delayTime = 5; // 100K
- break;
- }
- MmioWriteFun(MxSWI2CBUS, 0x0c);
- for ( index=0;index<9;index++ ) {
- MmioWriteFun(MxSWI2CBUS, 0x4c);
- udelay(delayTime);
- MmioWriteFun(MxSWI2CBUS, 0x48);
- udelay(delayTime);
- }
- MmioWriteFun(MxSWI2CBUS, 0x40);
- udelay(delayTime);
- MmioWriteFun(MxSWI2CBUS, 0x44);
- udelay(delayTime);
- MmioWriteFun(MxSWI2CBUS, 0x4c);
- udelay(delayTime);
- MmioWriteFun(MxSWI2CBUS, 0x0c);
- }
- void I2CRead(UINT8 MasterIndex,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed,UINT8 *pStatus)
- {
- UINT32 RegValue,ucStatus;
- PVOID MxControl0,MxControl1,__attribute__((unused)) MxData[2],MxAddress,MxDataLength;
- #if !INTERRUPT_ENABLE
- UINT32 Remainder,Completed;
- UINT8 CopyBytes,Switch=0;
- #endif
- UINT8 I2CRead_RETRY_CNT=0;
- // DebugPrint("DRV_I2C_ReadFun 0x%02x 0x%02x %d",DeviceID,Address,Length);
- /*
- if(MasterIndex>=MASTER_NUMBER || AddrType>=ADDRESS_TYPE_INVALID ||
- (AddrType==ADDRESS_TYPE_MULTIPLE&&(Address&0x03000000)==0) ||
- Length == 0 || pData == NULL)
- {
- DbgFunPrint("Invalid Parameter");
- *pStatus = I2C_STATUS_DATA_ERROR;
- return;
- }
- if(in_atomic())
- {
- DbgFunPrint("ID=%02x,Invalid Process!!",DeviceID);
- *pStatus = I2C_STATUS_DATA_ERROR;
- return;
- }
- down(&I2cDev.I2cMutex[MasterIndex]);
- */
-
- /* if(down_interruptible(&I2cDev.I2cMutex[MasterIndex]))
- {
- DbgFunPrint("SYSTEM_ERROR");
- *pStatus = I2C_STATUS_SYSTEM_ERROR;
- return;
- }*/
- #if 0
- if (bSuspend)
- {
- DbgFunPrint("System is suspend!!!!!!!");
- // up(&I2cDev.I2cMutex[MasterIndex]);
- *pStatus = I2C_STATUS_SYSTEM_ERROR;
- return;
- }
- #endif
- I2CRead_retry:
- if(g_ucHangNum>1)
- g_BusyLoop = 2;
- else
- g_BusyLoop = 100;
-
- RegValue = 0;
- // BusyCounter = 0;
- if(MasterIndex==0)
- {
- MxControl0 = (PVOID)M0_CONTROL0;
- MxControl1 = (PVOID)M0_CONTROL1;
- MxData[0] = (PVOID)M0_DATA0;
- MxData[1] = (PVOID)M0_DATA1;
- MxAddress = (PVOID)M0_BASE_ADDRESS;
- MxDataLength = (PVOID)M0_DATA_LENGTH;
- M0BusyCounter = 0;
- while((MmioReadFun(M0_BUS_STATUS) & I2C_BUS_STATUS_BUSY)==I2C_BUS_STATUS_BUSY)
- {
- //DbgFunPrint("I2C0BusBusy 0x%08x %d",MmioReadFun(M0_BUS_STATUS),RegValue++);
- M0BusyCounter++;
- DbgFunPrint("I2C0BusBusy 0x%08x,BusyCount=%d",MmioReadFun(M0_BUS_STATUS),M0BusyCounter);
- msleep(10);
- if (M0BusyCounter > g_BusyLoop)
- {
- // printk(KERN_EMERG "[I2C WARNING]I2C0BusBusy!!!!!!!!!!!!\n");
- *pStatus = I2C_STATUS_DATA_ERROR;
- // up(&I2cDev.I2cMutex[MasterIndex]);
- // if(1000 == g_BusyLoop)
- {
- g_ucHangNum++;
- if(0==(g_ucHangNum%10))
- {
- printk("<0>""[I2C][%s]0_I2C BUS BUSY!!,g_ucHangNum=%d\n",__FUNCTION__,g_ucHangNum);
- printk("<0>""MasterIndex = %d",MasterIndex);
- printk("<0>""DeviceID = 0x%x",DeviceID);
- printk("<0>""AddrType = %d",AddrType);
- printk("<0>""Address = %d",Address);
- printk("<0>""Length = %d",Length);
- printk("<0>""Speed = %d",Speed);
- }
- }
- return;
- //break;
- }
- }
- g_ucHangNum = 0;
-
- }
- else// if(MasterIndex==1)
- {
- MxControl0 = (PVOID)M1_CONTROL0;
- MxControl1 = (PVOID)M1_CONTROL1;
- MxData[0] = (PVOID)M1_DATA0;
- MxData[1] = (PVOID)M1_DATA1;
- MxAddress = (PVOID)M1_BASS_ADDRESS;
- MxDataLength = (PVOID)M1_DATA_LENGTH;
- M1BusyCounter = 0;
- while((MmioReadFun(M1_BUS_STATUS) & I2C_BUS_STATUS_BUSY)==I2C_BUS_STATUS_BUSY)
- {
- //DbgFunPrint("I2C1BusBusy %d",RegValue++);
- M1BusyCounter++;
- DbgFunPrint("I2C1BusBusy,BusyCount=%d",M1BusyCounter);
- #if 0//UMF_OPT_INTERNETMONITOR_SPEC
-
- #else
- msleep(10);
- #endif
-
- if (M1BusyCounter > g_BusyLoop)
- {
- // printk(KERN_EMERG "[I2C WARNING]I2C1BusBusy!!!!!!!!!!!!\n");
- *pStatus = I2C_STATUS_DATA_ERROR;
- // up(&I2cDev.I2cMutex[MasterIndex]);
- // if(1000 == g_BusyLoop)
- {
- g_ucHangNum++;
- if(0==(g_ucHangNum%10))
- {
- printk("<0>""[I2C][%s]0_I2C BUS BUSY!!,g_ucHangNum=%d\n",__FUNCTION__,g_ucHangNum);
- printk("<0>""MasterIndex = %d",MasterIndex);
- printk("<0>""DeviceID = 0x%x",DeviceID);
- printk("<0>""AddrType = %d",AddrType);
- printk("<0>""Address = %d",Address);
- printk("<0>""Length = %d",Length);
- printk("<0>""Speed = %d",Speed);
- }
- }
- return;
- //break;
- }
- }
- g_ucHangNum = 0;
- }
- MasterAsyncResetFun(MasterIndex,Speed); // Asynchronous reset
- // to avoid warning message
- // MxData[0] = MxData[0];
- // MxData[1] = MxData[1];
- // To Do
- DeviceID &= 0xFE;
- if(AddrType==ADDRESS_TYPE_BYTE)
- {
- RegValue = ((UINT32)Length)<<16;
- MmioWriteFun(MxDataLength,RegValue); // Data Length
- RegValue = 0x01000000 | (Address & 0xFF);
- MmioWriteFun(MxAddress,RegValue); // Address
- RegValue = ((UINT32)DeviceID) | (((UINT32)DeviceID | 0x01)<<16);
- MmioWriteFun(MxControl1,RegValue);
- RegValue = 0x03800000;
- //I2cDev.InterruptCounter[MasterIndex] = 1;
- }
- /* else if(AddrType==ADDRESS_TYPE_WORD)
- {
- }*/
- else if(AddrType==ADDRESS_TYPE_NONE)
- {
- MmioWriteFun(MxDataLength,(UINT32)Length); // Data Length
- RegValue = ((UINT32)DeviceID | 0x01);
- MmioWriteFun(MxControl1,RegValue);
- RegValue = 0x02800000;
- // RegValue = 0x02800000 | MX_CONTROL0_NO_ADDRESS;
- //I2cDev.InterruptCounter[MasterIndex] = 0;
- }
- else if(AddrType==ADDRESS_TYPE_MULTIPLE)
- {
- RegValue = ((UINT32)Length)<<16;
- MmioWriteFun(MxDataLength,RegValue); // Data Length
- MmioWriteFun(MxAddress,Address&0x03FFFFFF); // Address
- RegValue = ((UINT32)DeviceID) | (((UINT32)DeviceID | 0x01)<<16);
- MmioWriteFun(MxControl1,RegValue);
- RegValue = 0x03800000;
- //I2cDev.InterruptCounter[MasterIndex] = 1;
- }
- I2cDev.InterruptCounter[MasterIndex] = 0;
- if(Speed==I2C_SPEED_25K)
- {
- RegValue |= MX_CONTROL0_SCL_25K;
- }
- else if(Speed==I2C_SPEED_100K)
- {
- RegValue |= MX_CONTROL0_SCL_100K;
- }
- else if(Speed==I2C_SPEED_200K)
- {
- RegValue |= MX_CONTROL0_SCL_200K;
- }
- else if(Speed==I2C_SPEED_300K)
- {
- RegValue |= MX_CONTROL0_SCL_300K;
- }
- else if(Speed==I2C_SPEED_400K)
- {
- RegValue |= MX_CONTROL0_SCL_400K;
- }
- I2cDev.Status[MasterIndex] = I2C_STATUS_SUCCESS;
- #if INTERRUPT_ENABLE
- I2cDev.IntInfo[MasterIndex].pBuffer = pData;
- I2cDev.IntInfo[MasterIndex].Length = Length;
- I2cDev.IntInfo[MasterIndex].Completed = 0;
- I2cDev.IntInfo[MasterIndex].bRead = TRUE;
- #if ONE_BYTE_INTERRUPT
- I2cDev.InterruptCounter[MasterIndex] += Length;
- RegValue |= MX_CONTROL0_INT_EN|MX_CONTROL0_ONE_BYTE_INT|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE;
- #else
- I2cDev.InterruptCounter[MasterIndex] += (Length+3)/4;
- RegValue |= MX_CONTROL0_INT_EN|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE;
- #endif
- //if(MasterIndex==0)
- // mod_timer(&I2cDev.ResetTimer[0],jiffies+(Length+49)/50*(HZ/25));
- wmb();
- MmioWriteFun(MxControl0,RegValue);
- //20120720 DylanWang, use timeout to avoid watch dog reboot the system
- //wait 2s
- //wait_for_completion(&I2cDev.Comp[MasterIndex]);
- ucStatus = wait_for_completion_timeout(&I2cDev.Comp[MasterIndex], 2*HZ);
- if(ucStatus<2)
- {
- g_ucHangNum++;
- printk("<0>""[I2C][%s]wait completion timeout!!\n",__FUNCTION__);
- *pStatus = I2C_STATUS_SYSTEM_TIMEOUT;
- #if 1
- SW_I2C_DummyRead(MasterIndex, Speed);
- udelay(100*1000);//wait 100 ms to clear remain interrupts
- init_completion(&I2cDev.Comp[MasterIndex]); //reset complete count
- if(I2CRead_RETRY_CNT == 0)
- {
- I2CRead_RETRY_CNT += 1;
- goto I2CRead_retry;
- }
- #else
- return;
- #endif
- }
- #if 0
- if (bSuspend)
- {
- DbgFunPrint("System is suspend!!!!!!!");
- // up(&I2cDev.I2cMutex[MasterIndex]);
- *pStatus = I2C_STATUS_SYSTEM_ERROR;
- return;
- }
- #endif
- MmioWriteFun(0xbe070054,500); //20110120 lwhite add 20us delay between two commands (stop and next start)
-
- MmioWriteFun(0xbe110054,500);
- /* if(I2cDev.Status[MasterIndex]!=I2C_STATUS_SUCCESS)
- {
- DbgFunPrint("I2C Read %d/%d",I2cDev.IntInfo[MasterIndex].Completed,Length);
- }*/
- #else // else of #if INTERRUPT_ENABLE
- Completed = 0;
- Remainder = Length;
- I2cDev.InterruptCounter[MasterIndex] += Length;
- RegValue |= MX_CONTROL0_ONE_BYTE_INT;
- wmb();
- MmioWriteFun(MxControl0,RegValue);
- if(Length>4)
- {
- CopyBytes = 4;
- }
- else
- {
- CopyBytes = Length;
- }
- MasterPollingFun(MasterIndex,CopyBytes+1);
- RegValue = MmioReadFun(MxData[Switch]);
- Switch ^= 1;
- memcpy(&pData[Completed],&RegValue,CopyBytes);
- Completed += CopyBytes;
- Remainder -= CopyBytes;
- while(Remainder > 0)
- {
- if(Remainder>4)
- {
- CopyBytes = 4;
- }
- else
- {
- CopyBytes = Remainder;
- }
- MasterPollingFun(MasterIndex,CopyBytes);
- RegValue = MmioReadFun(MxData[Switch]);
- Switch ^= 1;
- memcpy(&pData[Completed],&RegValue,CopyBytes);
- Remainder -= CopyBytes;
- Completed += CopyBytes;
- }
- #endif // end of #if INTERRUPT_ENABLE
- *pStatus = I2cDev.Status[MasterIndex];
- }
- void DRV_I2C_ReadFun( UINT8 MasterIndex,
- UINT8 DeviceID,
- UINT8 AddrType,
- UINT32 Address,
- UINT8 *pData,
- UINT16 Length,
- UINT8 Speed,
- UINT8 *pStatus)
- {
- if(MasterIndex>=MASTER_NUMBER || AddrType>=ADDRESS_TYPE_INVALID ||
- (AddrType==ADDRESS_TYPE_MULTIPLE&&(Address&0x03000000)==0) ||
- Length == 0 || pData == NULL)
- {
- DbgFunPrint("Invalid Parameter");
- *pStatus = I2C_STATUS_DATA_ERROR;
- return;
- }
- if(in_atomic())
- {
- DbgFunPrint("ID=%02x,Invalid Process!!",DeviceID);
- *pStatus = I2C_STATUS_DATA_ERROR;
- return;
- }
- down(&I2cDev.I2cMutex[MasterIndex]);
- I2CRead(MasterIndex,DeviceID,AddrType,Address,pData,Length,Speed,pStatus);
- up(&I2cDev.I2cMutex[MasterIndex]);
- }
- void I2CWrite(UINT8 MasterIndex,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed,UINT8 *pStatus)
- {
- UINT32 RegValue,Completed,ucStatus;
- PVOID MxControl0,MxControl1,MxData[2],MxAddress,MxDataLength;
- UINT8 __attribute__((unused)) Remainder,CopyBytes;
- #if !INTERRUPT_ENABLE
- UINT8 Switch=0;
- #endif
- // DebugPrint("DRV_I2C_WriteFun 0x%02x 0x%02x %d",DeviceID,Address,Length);
- /*
- if(MasterIndex>=MASTER_NUMBER || AddrType>=ADDRESS_TYPE_INVALID ||
- (AddrType==ADDRESS_TYPE_MULTIPLE&&(Address&0x03000000)==0) ||
- (Length!=0 && pData==NULL) || (Length==0 && AddrType==ADDRESS_TYPE_NONE) )
- {
- DbgFunPrint("Invalid Parameter");
- *pStatus = I2C_STATUS_DATA_ERROR;
- return;
- }
- if(in_atomic())
- {
- if(QueueI2CFun(MasterIndex,DeviceID,AddrType,Address,pData,Length,Speed)<0)
- {
- DbgFunPrint("Invalid Process!!");
- *pStatus = I2C_STATUS_DATA_ERROR;
- }
- else
- {
- *pStatus = I2C_STATUS_SUCCESS;
- }
- return;
- }
- down(&I2cDev.I2cMutex[MasterIndex]);
- */
-
- /* if(down_interruptible(&I2cDev.I2cMutex[MasterIndex]))
- {
- DbgFunPrint("SYSTEM_ERROR");
- *pStatus = I2C_STATUS_SYSTEM_ERROR;
- return;
- }*/
- #if 0
- if (bSuspend)
- {
- DbgFunPrint("System is suspend!!!!!!!");
- // up(&I2cDev.I2cMutex[MasterIndex]);
- *pStatus = I2C_STATUS_SYSTEM_ERROR;
- return;
- }
- #endif
- if(g_ucHangNum>1)
- g_BusyLoop = 2;
- else
- g_BusyLoop = 100;
-
- // BusyCounter = 0;
- if(MasterIndex==0)
- {
- MxControl0 = (PVOID)M0_CONTROL0;
- MxControl1 = (PVOID)M0_CONTROL1;
- MxData[0] = (PVOID)M0_DATA0;
- MxData[1] = (PVOID)M0_DATA1;
- MxAddress = (PVOID)M0_BASE_ADDRESS;
- MxDataLength = (PVOID)M0_DATA_LENGTH;
- M0BusyCounter = 0;
- while((MmioReadFun(M0_BUS_STATUS) & I2C_BUS_STATUS_BUSY)==I2C_BUS_STATUS_BUSY)
- {
- //DbgFunPrint("I2C0BusBusy %d",RegValue++);
- M0BusyCounter++;
- DbgFunPrint("I2C0BusBusy,BusyCount=%d",M0BusyCounter);
- msleep(10);
-
- if (M0BusyCounter > g_BusyLoop)
- {
- // printk(KERN_EMERG "[I2C WARNING]I2C0BusBusy!!!!!!!!!!!!\n");
- *pStatus = I2C_STATUS_DATA_ERROR;
- // up(&I2cDev.I2cMutex[MasterIndex]);
- // if(1000 == g_BusyLoop)
- {
- g_ucHangNum++;
- if(0==(g_ucHangNum%10))
- {
- printk("<0>""[I2C][%s]0_I2C BUS BUSY!!,g_ucHangNum=%d\n",__FUNCTION__,g_ucHangNum);
- printk("<0>""MasterIndex = %d",MasterIndex);
- printk("<0>""DeviceID = 0x%x",DeviceID);
- printk("<0>""AddrType = %d",AddrType);
- printk("<0>""Address = %d",Address);
- printk("<0>""Length = %d",Length);
- printk("<0>""Speed = %d",Speed);
- }
- }
- return;
- //break;
- }
- }
- g_ucHangNum = 0;
-
- }
- else// if(MasterIndex==1)
- {
- MxControl0 = (PVOID)M1_CONTROL0;
- MxControl1 = (PVOID)M1_CONTROL1;
- MxData[0] = (PVOID)M1_DATA0;
- MxData[1] = (PVOID)M1_DATA1;
- MxAddress = (PVOID)M1_BASS_ADDRESS;
- MxDataLength = (PVOID)M1_DATA_LENGTH;
- M1BusyCounter = 0;
- while((MmioReadFun(M1_BUS_STATUS) & I2C_BUS_STATUS_BUSY)==I2C_BUS_STATUS_BUSY)
- {
- //DbgFunPrint("I2C1BusBusy %d",RegValue++);
- M1BusyCounter++;
- DbgFunPrint("I2C1BusBusy,BusyCount=%d",M1BusyCounter);
- #if 0 //UMF_OPT_INTERNETMONITOR_SPEC
-
- #else
- msleep(10);
- #endif
-
- if (M1BusyCounter > g_BusyLoop)
- {
- // printk(KERN_EMERG "[I2C WARNING]I2C1BusBusy!!!!!!!!!!!!\n");
- *pStatus = I2C_STATUS_DATA_ERROR;
- // up(&I2cDev.I2cMutex[MasterIndex]);
- // if(1000 == g_BusyLoop)
- {
- g_ucHangNum++;
- if(0==(g_ucHangNum%10))
- {
- printk("<0>""[I2C][%s]0_I2C BUS BUSY!!,g_ucHangNum=%d\n",__FUNCTION__,g_ucHangNum);
- printk("<0>""MasterIndex = %d",MasterIndex);
- printk("<0>""DeviceID = 0x%x",DeviceID);
- printk("<0>""AddrType = %d",AddrType);
- printk("<0>""Address = %d",Address);
- printk("<0>""Length = %d",Length);
- printk("<0>""Speed = %d",Speed);
- }
- }
- return;
- //break;
- }
- }
- g_ucHangNum = 0;
-
- }
- MasterAsyncResetFun(MasterIndex,Speed); // Asynchronous reset
- // To Do
- Completed = 0;
- if(Length == 0)
- {
- if(AddrType==ADDRESS_TYPE_MULTIPLE)
- {
- CopyBytes = (UINT8)(Address>>24)&3;
- }
- else
- {
- CopyBytes = 1;
- }
- Completed = CopyBytes;
- Length = CopyBytes;
- Remainder = 0;
- memcpy(&RegValue,&Address,CopyBytes);
- MmioWriteFun(MxData[0],RegValue); // Data
- AddrType = ADDRESS_TYPE_NONE;
- }
- else
- {
- if(Length > 4)
- {
- Remainder = Length - 4;
- CopyBytes = 4;
- }
- else
- {
- Remainder = 0;
- CopyBytes = Length;
- }
- memcpy(&RegValue,&pData[Completed],CopyBytes);
- Completed += CopyBytes;
- MmioWriteFun(MxData[0],RegValue); // Data
- if(Remainder > 0)
- {
- if(Remainder > 4)
- {
- CopyBytes = 4;
- }
- else
- {
- CopyBytes = Remainder;
- }
- Remainder -= CopyBytes;
- memcpy(&RegValue,&pData[Completed],CopyBytes);
- Completed += CopyBytes;
- MmioWriteFun(MxData[1],RegValue); // Data
- }
- }
- if(AddrType==ADDRESS_TYPE_BYTE)
- {
- MmioWriteFun(MxDataLength,(UINT32)Length); // Data Length
- RegValue = 0x01000000 | (Address & 0xFF);
- MmioWriteFun(MxAddress,RegValue); // Address
- RegValue = (UINT32)DeviceID&0xFE;
- MmioWriteFun(MxControl1,RegValue);
- RegValue = 0x02800000;
- //I2cDev.InterruptCounter[MasterIndex] = 1;
- }
- /* else if(AddrType==ADDRESS_TYPE_WORD)
- {
- RegValue = 0x02800000;
- }*/
- else if(AddrType==ADDRESS_TYPE_NONE)
- {
- MmioWriteFun(MxDataLength,(UINT32)Length); // Data Length
- RegValue = (UINT32)DeviceID&0xFE;
- MmioWriteFun(MxControl1,RegValue);
- RegValue = 0x02800000 | MX_CONTROL0_NO_ADDRESS;
- //I2cDev.InterruptCounter[MasterIndex] = 0;
- }
- else if(AddrType==ADDRESS_TYPE_MULTIPLE)
- {
- RegValue = ((UINT32)Length)<<16;
- MmioWriteFun(MxDataLength,RegValue); // Data Length
- MmioWriteFun(MxAddress,Address&0x03FFFFFF); // Address
- RegValue = ((UINT32)DeviceID&0xFE) | (((UINT32)DeviceID&0xFE)<<16);
- MmioWriteFun(MxControl1,RegValue);
- RegValue = 0x03800000;
- //I2cDev.InterruptCounter[MasterIndex] = 1;
- }
- I2cDev.InterruptCounter[MasterIndex] = 0;
- if(Speed==I2C_SPEED_25K)
- {
- RegValue |= MX_CONTROL0_SCL_25K;
- }
- else if(Speed==I2C_SPEED_100K)
- {
- RegValue |= MX_CONTROL0_SCL_100K;
- }
- else if(Speed==I2C_SPEED_200K)
- {
- RegValue |= MX_CONTROL0_SCL_200K;
- }
- else if(Speed==I2C_SPEED_300K)
- {
- RegValue |= MX_CONTROL0_SCL_300K;
- }
- else if(Speed==I2C_SPEED_400K)
- {
- RegValue |= MX_CONTROL0_SCL_400K;
- }
- I2cDev.Status[MasterIndex] = I2C_STATUS_SUCCESS;
- #if INTERRUPT_ENABLE
- I2cDev.IntInfo[MasterIndex].pBuffer = pData;
- I2cDev.IntInfo[MasterIndex].Length = Length;
- I2cDev.IntInfo[MasterIndex].Completed = Completed;
- I2cDev.IntInfo[MasterIndex].bRead = FALSE;
- #if ONE_BYTE_INTERRUPT
- I2cDev.InterruptCounter[MasterIndex] += Length;
- RegValue |= MX_CONTROL0_INT_EN|MX_CONTROL0_ONE_BYTE_INT|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE;
- #else
- I2cDev.InterruptCounter[MasterIndex] += (Length+3)/4;
- RegValue |= MX_CONTROL0_INT_EN|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE;
- #endif
- //if(MasterIndex==0)
- // mod_timer(&I2cDev.ResetTimer[0],jiffies+(Length+49)/50*(HZ/25));
- wmb();
- MmioWriteFun(MxControl0,RegValue);
-
- //20120720 DylanWang, use timeout to avoid watch dog reboot the system
- //wait 2s
- //wait_for_completion(&I2cDev.Comp[MasterIndex]);
- ucStatus = wait_for_completion_timeout(&I2cDev.Comp[MasterIndex], 2*HZ);
- if(ucStatus<2)
- {
- g_ucHangNum++;
- printk("<0>""[I2C][%s]wait completion timeout!!\n",__FUNCTION__);
- *pStatus = I2C_STATUS_SYSTEM_TIMEOUT;
- return;
- }
- #if 0
- if (bSuspend)
- {
- DbgFunPrint("System is suspend!!!!!!!");
- // up(&I2cDev.I2cMutex[MasterIndex]);
- *pStatus = I2C_STATUS_SYSTEM_ERROR;
- return;
- }
- #endif
- MmioWriteFun(0xbe070054,500); //20110120 lwhite add 20us delay between two commands (stop and next start)
-
- MmioWriteFun(0xbe110054,500);
- /* if(I2cDev.Status[MasterIndex]!=I2C_STATUS_SUCCESS)
- {
- DbgFunPrint("I2C Write %d/%d",I2cDev.IntInfo[MasterIndex].Completed,Length);
- }*/
- #else // else of #if INTERRUPT_ENABLE
- I2cDev.InterruptCounter[MasterIndex] += Length;
- RegValue |= MX_CONTROL0_ONE_BYTE_INT;
- wmb();
- MmioWriteFun(MxControl0,RegValue);
- if(Length>4)
- {
- if(AddrType==ADDRESS_TYPE_NONE)
- {
- MasterPollingFun(MasterIndex,4);
- }
- else
- {
- MasterPollingFun(MasterIndex,5);
- }
- }
- else
- {
- if(AddrType==ADDRESS_TYPE_NONE)
- {
- MasterPollingFun(MasterIndex,Length);
- }
- else
- {
- MasterPollingFun(MasterIndex,Length+1);
- }
- }
- while(Remainder > 0)
- {
- if(Remainder>4)
- {
- CopyBytes = 4;
- }
- else
- {
- CopyBytes = Remainder;
- }
- memcpy(&RegValue,&pData[Completed],CopyBytes);
- MmioWriteFun(MxData[Switch],RegValue); // Data
- Switch ^= 1;
- Remainder -= CopyBytes;
- Completed += CopyBytes;
- MasterPollingFun(MasterIndex,CopyBytes);
- }
- MasterPollingFun(MasterIndex,I2cDev.InterruptCounter[MasterIndex]);
- #endif // end of #if INTERRUPT_ENABLE
- *pStatus = I2cDev.Status[MasterIndex];
- }
- void DRV_I2C_WriteFun( UINT8 MasterIndex,
- UINT8 DeviceID ,
- UINT8 AddrType ,
- UINT32 Address ,
- UINT8 *pData ,
- UINT16 Length ,
- UINT8 Speed ,
- UINT8 *pStatus)
- {
- if(MasterIndex>=MASTER_NUMBER || AddrType>=ADDRESS_TYPE_INVALID ||
- (AddrType==ADDRESS_TYPE_MULTIPLE&&(Address&0x03000000)==0) ||
- (Length!=0 && pData==NULL) || (Length==0 && AddrType==ADDRESS_TYPE_NONE) )
- {
- DbgFunPrint("Invalid Parameter");
- *pStatus = I2C_STATUS_DATA_ERROR;
- return;
- }
- if(in_atomic())
- {
- if(QueueI2CFun(MasterIndex,DeviceID,AddrType,Address,pData,Length,Speed)<0)
- {
- DbgFunPrint("Invalid Process!!");
- *pStatus = I2C_STATUS_DATA_ERROR;
- }
- else
- {
- *pStatus = I2C_STATUS_SUCCESS;
- }
- return;
- }
- down(&I2cDev.I2cMutex[MasterIndex]);
- I2CWrite(MasterIndex,DeviceID,AddrType,Address,pData,Length,Speed,pStatus);
- up(&I2cDev.I2cMutex[MasterIndex]);
- }
- UINT32 DRV_I2C_M0BasicReadFun(UINT8 DeviceID,UINT8 Address,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
- {
- UINT32 RegValue;
- // DebugPrint("DRV_I2C_M0BasicReadFun 0x%02x 0x%02x %d %d",DeviceID,Address,Length,Speed);
- if(Length>4)
- {
- *pStatus = I2C_STATUS_DATA_ERROR;
- DbgFunPrint("Invalid Parameter");
- return -1;
- }
- DRV_I2C_ReadFun(0,DeviceID,ADDRESS_TYPE_BYTE,Address,(UINT8*)(&RegValue),(UINT32)(Length),Speed,(UINT8*)(pStatus));
- return RegValue;
- }
- void DRV_I2C_M0BasicWriteFun(UINT8 DeviceID,UINT8 Address,UINT32 Data,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
- {
- // DebugPrint("DRV_I2C_M0BasicWriteFun 0x%02x 0x%02x 0x%08x %d %d",DeviceID,Address,Data,Length,Speed);
- if(Length>4)
- {
- *pStatus = I2C_STATUS_DATA_ERROR;
- DbgFunPrint("Invalid Parameter");
- return;
- }
- DRV_I2C_WriteFun(0,DeviceID,ADDRESS_TYPE_BYTE,(UINT32)(Address),(UINT8*)(&Data),(UINT16)(Length),Speed,(UINT8*)(pStatus));
- }
- UINT32 DRV_I2C_M1BasicReadFun(UINT8 DeviceID,UINT8 Address,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
- {
- UINT32 RegValue;
- // DebugPrint("DRV_I2C_M1BasicReadFun 0x%02x 0x%02x %d %d",DeviceID,Address,Length,Speed);
- if(Length>4)
- {
- *pStatus = I2C_STATUS_DATA_ERROR;
- DbgFunPrint("Invalid Parameter");
- return -1;
- }
- DRV_I2C_ReadFun(1,DeviceID,ADDRESS_TYPE_BYTE,Address,(UINT8*)(&RegValue),(UINT32)(Length),Speed,(UINT8*)(pStatus));
- return RegValue;
- }
- void DRV_I2C_M1BasicWriteFun(UINT8 DeviceID,UINT8 Address,UINT32 Data,UINT8 Length,UINT8 Speed,UINT8 *pStatus)
- {
- // DebugPrint("DRV_I2C_M1BasicWriteFun 0x%02x 0x%02x 0x%08x %d %d",DeviceID,Address,Data,Length,Speed);
- if(Length>4)
- {
- *pStatus = I2C_STATUS_DATA_ERROR;
- DbgFunPrint("Invalid Parameter");
- return;
- }
-
- DRV_I2C_WriteFun(1,DeviceID,ADDRESS_TYPE_BYTE,(UINT32)(Address),(UINT8*)(&Data),(UINT16)(Length),Speed,(UINT8*)(pStatus));
- }
- #endif
- INT32 DRV_I2C_QueueCmdFun(UINT8 Mx,UINT8 DeviceID,UINT8 AddrType,UINT32 Address,UINT8 *pData,UINT16 Length,UINT8 Speed, INT8 bRead)
- {
- PI2C_CMD_LIST pI2CCmdList;
- //printk(KERN_INFO "[lwhite] DRV_I2C_QueueCmdFun===>\n");
-
- pI2CCmdList = drv_kmalloc(sizeof(I2C_CMD_LIST)+Length-1,GFP_ATOMIC, MODULEID_I2C); // Cannot sleep
-
- if(!pI2CCmdList)
- {
- DbgFunPrint("Cannot allocate I2C_CMD_LIST");
- return -ENOMEM;
- }
- if (bRead)
- {
- pI2CCmdList->bRead = bRead;
- pI2CCmdList->Mx = Mx;
- pI2CCmdList->DeviceID = DeviceID;
- pI2CCmdList->AddrType = AddrType;
- pI2CCmdList->Speed = Speed;
- pI2CCmdList->Address = Address;
- pI2CCmdList->Length = Length;
- }
- else
- {
- pI2CCmdList->bRead = bRead;
- pI2CCmdList->Mx = Mx;
- pI2CCmdList->DeviceID = DeviceID;
- pI2CCmdList->AddrType = AddrType;
- pI2CCmdList->Speed = Speed;
- pI2CCmdList->Address = Address;
- pI2CCmdList->Length = Length;
- memcpy(pI2CCmdList->Data,pData,Length);
- }
- list_add_tail(&pI2CCmdList->list, &CmdList);
- //printk(KERN_INFO "[lwhite] DRV_I2C_QueueCmdFun<===\n");
- return 0;
- }
- INT32 DRV_I2C_RunCmdFun(UINT8 Mx,UINT8 *pData)
- {
- struct list_head QueueList,*cursor = NULL,*next = NULL;
- PI2C_CMD_LIST pQueue;
- UINT8 Status;
- INT8 bLast = 0;
- INT8 result= 0;
- //printk(KERN_INFO "[lwhite] DRV_I2C_RunCmdFun===>\n");
- if (Mx > 1)
- {
- DbgFunPrint("[lwhite] DRV_I2C_RunCmdFun ERROR===> Mx error!!!!");
- result = 1;
- return result;
- }
- down(&I2cDev.I2cMutex[Mx]);
-
- INIT_LIST_HEAD(&QueueList);
- list_splice_init(&CmdList,&QueueList);
- list_for_each_safe(cursor,next,&QueueList)
- {
-
- if (cmd_list_is_last(cursor,&QueueList))
- {
- //printk(KERN_INFO "[lwhite] DRV_I2C_RunCmdFun===>this is the last one!!!!\n");
- bLast = 1;
- }
-
-
- list_del(cursor);
- pQueue = container_of(cursor,I2C_CMD_LIST,list);
- //Status = I2C_STATUS_SUCCESS;
- if (pQueue->bRead && bLast)
- {
- I2CRead(pQueue->Mx
- ,pQueue->DeviceID
- ,pQueue->AddrType
- ,pQueue->Address
- ,pQueue->Data,pQueue->Length
- ,pQueue->Speed,&Status);
- if (Status)
- {
- result = 1;
- DbgFunPrint("[lwhite] DRV_I2C_RunCmdFun ERROR===> Read Status error!!!!");
- }
- //printk(KERN_EMERG "[lwhite] DRV_I2C_RunCmdFun===> pQueue->Data[0]=0x%02x\n",pQueue->Data[0]);
- memcpy(pData,&pQueue->Data,pQueue->Length);
- //printk(KERN_EMERG "[lwhite] DRV_I2C_RunCmdFun===> pData[0]=0x%02x\n",pData[0]);
-
- }
- else if (pQueue->bRead && bLast==0)
- {
- DbgFunPrint("[lwhite] DRV_I2C_RunCmdFun ERROR===> the Read Command is not the last one!!!!");
- result = 1;
-
- }
- else
- {
- I2CWrite(pQueue->Mx
- ,pQueue->DeviceID
- ,pQueue->AddrType
- ,pQueue->Address
- ,pQueue->Data,pQueue->Length
- ,pQueue->Speed,&Status);
- if (Status)
- {
- result = 1;
- DbgFunPrint("[lwhite] DRV_I2C_RunCmdFun ERROR===> Write Status error!!!!");
- }
- }
- drv_kfree(pQueue, MODULEID_I2C);
- }
- //printk(KERN_INFO "[lwhite] DRV_I2C_RunCmdFun<===\n");
- up(&I2cDev.I2cMutex[Mx]);
- return result;
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,8)
- static long I2cIoctlFun(struct file *file, unsigned int cmd, unsigned long arg)
- #else
- static int I2cIoctlFun(struct inode *inode, struct file *pFile,unsigned int cmd, unsigned long arg)
- #endif
- {
- // PI2C_DEV pDev;
- INT32 retval;
- UINT8 Status;
- // DebugPrint("I2cIoctlFun");
- if (_IOC_TYPE(cmd) != I2C_IOC_MAGIC)
- {
- DbgFunPrint("Invalid command");
- return -ENOTTY;
- }
- // pDev = (PI2C_DEV)pFile->private_data;
- retval = 0;
- Status = 0;
- switch(cmd)
- {
- //#ifndef RELEASE_VER
- case IOC_I2C_MASTER0_WRITE_BYTE_100K:
- {
- UINT8 Data[3];
- // DebugPrint("IOC_I2C_MASTER0_WRITE_BYTE_100K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M0_WRITE_BYTE_100K(Data[0],Data[1],Data[2],&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER0_READ_BYTE_100K:
- {
- UINT8 Data[2];
- // DebugPrint("IOC_I2C_MASTER0_READ_BYTE_100K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- Data[0] = DRV_I2C_M0_READ_BYTE_100K(Data[0],Data[1],&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data[0],(UINT8 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER0_WRITE_BYTE_400K:
- {
- UINT8 Data[3];
- // DebugPrint("IOC_I2C_MASTER0_WRITE_BYTE_400K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M0_WRITE_BYTE_400K(Data[0],Data[1],Data[2],&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER0_READ_BYTE_400K:
- {
- UINT8 Data[2];
- // DebugPrint("IOC_I2C_MASTER0_READ_BYTE_400K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- Data[0] = DRV_I2C_M0_READ_BYTE_400K(Data[0],Data[1],&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data[0],(UINT8 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_WRITE_BYTE_100K:
- {
- UINT8 Data[3];
- // DebugPrint("IOC_I2C_MASTER1_WRITE_BYTE_100K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M1_WRITE_BYTE_100K(Data[0],Data[1],Data[2],&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_READ_BYTE_100K:
- {
- UINT8 Data[2];
- // DebugPrint("IOC_I2C_MASTER1_READ_BYTE_100K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- Data[0] = DRV_I2C_M1_READ_BYTE_100K(Data[0],Data[1],&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data[0],(UINT8 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER0_WRITE_WORD_100K:
- {
- UINT8 Data[4];
- // DebugPrint("IOC_I2C_MASTER0_WRITE_WORD_100K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M0_WRITE_WORD_100K(Data[0],Data[1],*((UINT16 *)(&Data[2])),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER0_READ_WORD_100K:
- {
- UINT16 Data;
- // DebugPrint("IOC_I2C_MASTER0_READ_WORD_100K");
- if( get_user(Data,(UINT16 __user *)arg) )
- {
- DbgFunPrint("get_user fail");
- retval = -EFAULT;
- break;
- }
- Data = DRV_I2C_M0_READ_WORD_100K((UINT8)Data,(UINT8)(Data>>8),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data,(UINT16 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER0_WRITE_DWORD_100K:
- {
- UINT8 Data[6];
- // DebugPrint("IOC_I2C_MASTER0_WRITE_DWORD_100K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M0_WRITE_DWORD_100K(Data[0],Data[1],*((UINT32 *)(&Data[2])),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER0_READ_DWORD_100K:
- {
- UINT32 Data;
- // DebugPrint("IOC_I2C_MASTER0_READ_DWORD_100K");
- if( get_user(Data,(UINT32 __user *)arg) )
- {
- DbgFunPrint("get_user fail");
- retval = -EFAULT;
- break;
- }
- Data = DRV_I2C_M0_READ_DWORD_100K((UINT8)Data,(UINT8)(Data>>8),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data,(UINT32 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER0_WRITE_WORD_400K:
- {
- UINT8 Data[4];
- // DebugPrint("IOC_I2C_MASTER0_WRITE_WORD_400K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M0_WRITE_WORD_400K(Data[0],Data[1],*((UINT16 *)(&Data[2])),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER0_READ_WORD_400K:
- {
- UINT16 Data;
- // DebugPrint("IOC_I2C_MASTER0_READ_WORD_400K");
- if( get_user(Data,(UINT16 __user *)arg) )
- {
- DbgFunPrint("get_user fail");
- retval = -EFAULT;
- break;
- }
- Data = DRV_I2C_M0_READ_WORD_400K((UINT8)Data,(UINT8)(Data>>8),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data,(UINT16 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER0_WRITE_DWORD_400K:
- {
- UINT8 Data[6];
- // DebugPrint("IOC_I2C_MASTER0_WRITE_DWORD_400K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M0_WRITE_DWORD_400K(Data[0],Data[1],*((UINT32 *)(&Data[2])),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER0_READ_DWORD_400K:
- {
- UINT32 Data;
- // DebugPrint("IOC_I2C_MASTER0_READ_DWORD_400K");
- if( get_user(Data,(UINT32 __user *)arg) )
- {
- DbgFunPrint("get_user fail");
- retval = -EFAULT;
- break;
- }
- Data = DRV_I2C_M0_READ_DWORD_400K((UINT8)Data,(UINT8)(Data>>8),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data,(UINT32 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_WRITE_WORD_100K:
- {
- UINT8 Data[4];
- // DebugPrint("IOC_I2C_MASTER1_WRITE_WORD_100K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M1_WRITE_WORD_100K(Data[0],Data[1],*((UINT16 *)(&Data[2])),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_READ_WORD_100K:
- {
- UINT16 Data;
- // DebugPrint("IOC_I2C_MASTER1_READ_WORD_100K");
- if( get_user(Data,(UINT16 __user *)arg) )
- {
- DbgFunPrint("get_user fail");
- retval = -EFAULT;
- break;
- }
- Data = DRV_I2C_M1_READ_WORD_100K((UINT8)Data,(UINT8)(Data>>8),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data,(UINT16 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_WRITE_DWORD_100K:
- {
- UINT8 Data[6];
- // DebugPrint("IOC_I2C_MASTER1_WRITE_DWORD_100K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M1_WRITE_DWORD_100K(Data[0],Data[1],*((UINT32 *)(&Data[2])),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_READ_DWORD_100K:
- {
- UINT32 Data;
- // DebugPrint("IOC_I2C_MASTER1_READ_DWORD_100K");
- if( get_user(Data,(UINT32 __user *)arg) )
- {
- DbgFunPrint("get_user fail");
- retval = -EFAULT;
- break;
- }
- Data = DRV_I2C_M1_READ_DWORD_100K((UINT8)Data,(UINT8)(Data>>8),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data,(UINT32 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_WRITE_BYTE_400K:
- {
- UINT8 Data[3];
- // DebugPrint("IOC_I2C_MASTER1_WRITE_BYTE_400K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M1_WRITE_BYTE_400K(Data[0],Data[1],Data[2],&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_READ_BYTE_400K:
- {
- UINT8 Data[2];
- // DebugPrint("IOC_I2C_MASTER1_READ_BYTE_400K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- Data[0] = DRV_I2C_M1_READ_BYTE_400K(Data[0],Data[1],&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data[0],(UINT8 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_WRITE_WORD_400K:
- {
- UINT8 Data[4];
- // DebugPrint("IOC_I2C_MASTER1_WRITE_WORD_400K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M1_WRITE_WORD_400K(Data[0],Data[1],*((UINT16 *)(&Data[2])),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_READ_WORD_400K:
- {
- UINT16 Data;
- // DebugPrint("IOC_I2C_MASTER1_READ_WORD_400K");
- if( get_user(Data,(UINT16 __user *)arg) )
- {
- DbgFunPrint("get_user fail");
- retval = -EFAULT;
- break;
- }
- Data = DRV_I2C_M1_READ_WORD_400K((UINT8)Data,(UINT8)(Data>>8),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data,(UINT16 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_WRITE_DWORD_400K:
- {
- UINT8 Data[6];
- // DebugPrint("IOC_I2C_MASTER1_WRITE_DWORD_400K");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- DRV_I2C_M1_WRITE_DWORD_400K(Data[0],Data[1],*((UINT32 *)(&Data[2])),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_I2C_MASTER1_READ_DWORD_400K:
- {
- UINT32 Data;
- // DebugPrint("IOC_I2C_MASTER1_READ_DWORD_400K");
- if( get_user(Data,(UINT32 __user *)arg) )
- {
- DbgFunPrint("get_user fail");
- retval = -EFAULT;
- break;
- }
- Data = DRV_I2C_M1_READ_DWORD_400K((UINT8)Data,(UINT8)(Data>>8),&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- if( put_user(Data,(UINT32 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- #if SOFTWARE_MODE_MASTER
- #if 0
- case IOC_SW_I2C_MASTER0_WRITE_BYTE:
- {
- UINT8 Data[3];
- // DebugPrint("IOC_SW_I2C_MASTER0_WRITE_BYTE");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- SwI2CWriteBasicFun(0,Data[0],Data[1],Data[2],1,I2C_SPEED_100K,&Status);
- break;
- }
- case IOC_SW_I2C_MASTER0_READ_BYTE:
- {
- UINT8 Data[2];
- // DebugPrint("IOC_SW_I2C_MASTER0_READ_BYTE");
- if(copy_from_user(Data, (void __user *)arg, sizeof(Data)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- Data[0] = SwI2CReadBasicFun(0,Data[0],Data[1],1,I2C_SPEED_100K,&Status);
- if( put_user(Data[0],(UINT8 __user *)arg) )
- {
- DbgFunPrint("put_user fail");
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_SW_I2C_MASTER_READ_TEMP: // 20081112 lwhite test
- {
- I2C_TEMP_INFO I2cTempInfo;
- UINT8 *pData;
- if(copy_from_user(&I2cTempInfo, (void __user *)arg, sizeof(I2C_TEMP_INFO)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- pData = drv_kmalloc(I2cTempInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
- if(!pData)
- {
- DbgFunPrint("No resource");
- retval = -ENOMEM;
- break;
- }
- // memset(pData,0,I2cInfo.DataLength);
- I2CReadTempFun(I2cTempInfo.MasterIndex
- ,I2cTempInfo.SlaveAddress // DeviceID
- ,I2cTempInfo.BaseAddrType
- ,I2cTempInfo.BaseAddress // Offset
- ,I2cTempInfo.AddrLength
- ,pData, I2cTempInfo.DataLength
- ,I2cTempInfo.Speed
- ,&Status);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- if(copy_to_user((void __user *)I2cTempInfo.Data,pData,I2cTempInfo.DataLength))
- {
- DbgFunPrint("__copy_to_user fail");
- retval = -EFAULT;
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- #endif
- case IOC_SW_I2C_MASTER_READ:
- {
- I2C_INFO I2cInfo;
- UINT8 *pData;
- if(copy_from_user(&I2cInfo, (void __user *)arg, sizeof(I2C_INFO)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- pData = drv_kmalloc(I2cInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
- if(!pData)
- {
- DbgFunPrint("No resource");
- retval = -ENOMEM;
- break;
- }
- // memset(pData,0,I2cInfo.DataLength);
- DRV_SwI2C_ReadFun(I2cInfo.MasterIndex
- ,I2cInfo.SlaveAddress // DeviceID
- ,I2cInfo.BaseAddrType
- ,I2cInfo.BaseAddress // Offset
- ,pData, I2cInfo.DataLength
- ,I2cInfo.Speed
- ,&Status);
- // copy_to_user((void __user *)&I2cInfo.Status,Status,sizeof(Status));
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- if(copy_to_user((void __user *)I2cInfo.Data,pData,I2cInfo.DataLength))
- {
- DbgFunPrint("__copy_to_user fail");
- retval = -EFAULT;
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- #endif // SOFTWARE_MODE_MASTER
- #ifdef GPIO_I2C
- case IOC_GPIOI2C_MASTER_WRITE:
- {
- I2C_INFO I2cInfo;
- UINT8 *pData;
- if(copy_from_user(&I2cInfo, (void __user *)arg, sizeof(I2C_INFO)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- pData = drv_kmalloc(I2cInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
- if(!pData)
- {
- DbgFunPrint("No resource");
- retval = -ENOMEM;
- break;
- }
- if(copy_from_user(pData, (void __user *)I2cInfo.Data, I2cInfo.DataLength))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- DRV_GPIOI2C_WriteFun(I2cInfo.MasterIndex
- ,I2cInfo.SlaveAddress // DeviceID
- ,I2cInfo.BaseAddrType
- ,I2cInfo.BaseAddress // Offset
- ,pData, I2cInfo.DataLength
- ,I2cInfo.Speed
- ,&Status);
- // copy_to_user((void __user *)&I2cInfo.Status,Status,sizeof(Status));
- drv_kfree(pData, MODULEID_I2C);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- case IOC_GPIOI2C_MASTER_READ:
- {
- I2C_INFO I2cInfo;
- UINT8 *pData;
- if(copy_from_user(&I2cInfo, (void __user *)arg, sizeof(I2C_INFO)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- pData = drv_kmalloc(I2cInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
- if(!pData)
- {
- DbgFunPrint("No resource");
- retval = -ENOMEM;
- break;
- }
- // memset(pData,0,I2cInfo.DataLength);
- DRV_GPIOI2C_ReadFun(I2cInfo.MasterIndex
- ,I2cInfo.SlaveAddress // DeviceID
- ,I2cInfo.BaseAddrType
- ,I2cInfo.BaseAddress // Offset
- ,pData, I2cInfo.DataLength
- ,I2cInfo.Speed
- ,&Status);
- // copy_to_user((void __user *)&I2cInfo.Status,Status,sizeof(Status));
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- if(copy_to_user((void __user *)I2cInfo.Data,pData,I2cInfo.DataLength))
- {
- DbgFunPrint("__copy_to_user fail");
- retval = -EFAULT;
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- case IOC_I2C_MASTER_WRITE:
- {
- I2C_INFO I2cInfo;
- UINT8 *pData;
- if(copy_from_user(&I2cInfo, (void __user *)arg, sizeof(I2C_INFO)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- pData = drv_kmalloc(I2cInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
- if(!pData)
- {
- DbgFunPrint("No resource");
- retval = -ENOMEM;
- break;
- }
- if(copy_from_user(pData, (void __user *)I2cInfo.Data, I2cInfo.DataLength))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- DRV_I2C_WriteFun(I2cInfo.MasterIndex
- ,I2cInfo.SlaveAddress // DeviceID
- ,I2cInfo.BaseAddrType
- ,I2cInfo.BaseAddress // Offset
- ,pData, I2cInfo.DataLength
- ,I2cInfo.Speed
- ,&Status);
- // copy_to_user((void __user *)&I2cInfo.Status,Status,sizeof(Status));
- drv_kfree(pData, MODULEID_I2C);
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- break;
- }
- break;
- }
- #endif
- case IOC_I2C_MASTER_READ:
- {
- I2C_INFO I2cInfo;
- UINT8 *pData;
- if(copy_from_user(&I2cInfo, (void __user *)arg, sizeof(I2C_INFO)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- pData = drv_kmalloc(I2cInfo.DataLength, GFP_KERNEL, MODULEID_I2C);
- if(!pData)
- {
- DbgFunPrint("No resource");
- retval = -ENOMEM;
- break;
- }
- // memset(pData,0,I2cInfo.DataLength);
- DRV_I2C_ReadFun(I2cInfo.MasterIndex
- ,I2cInfo.SlaveAddress // DeviceID
- ,I2cInfo.BaseAddrType
- ,I2cInfo.BaseAddress // Offset
- ,pData, I2cInfo.DataLength
- ,I2cInfo.Speed
- ,&Status);
- // copy_to_user((void __user *)&I2cInfo.Status,Status,sizeof(Status));
- if(Status)
- {
- DbgFunPrint("Status Error:%d",Status);
- retval = -EFAULT;
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- if(copy_to_user((void __user *)I2cInfo.Data,pData,I2cInfo.DataLength))
- {
- DbgFunPrint("__copy_to_user fail");
- retval = -EFAULT;
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- drv_kfree(pData, MODULEID_I2C);
- break;
- }
- case IOC_I2C_DUMP_MEM:
- {
- I2C_DUMP I2cDump;
- UINT32 *Addr, Length,i;
- if(copy_from_user(&I2cDump, (void __user *)arg, sizeof(I2C_DUMP)))
- {
- DbgFunPrint("copy_from_user fail");
- retval = -EFAULT;
- break;
- }
- Addr = (UINT32 *)((UINT32)I2cDump.Addr & ~0x3); //4 bytes alignment
- Length = I2cDump.Length & ~0x3; //4 bytes alignment
- // DebugPrint("dump:Addr=0x%08x Length=%u",(UINT32)Addr,Length);
- if(Length==0)
- break;
- printk("????????????????????\n");
- for(i=0;i<Length;)
- {
- if((i&0xF)==0)
- printk("0x%08X: ",(UINT32)Addr);
- printk("%08X ",MmioReadFun(Addr));
- Addr++;
- i += 4;
- if((i&0xF)==0)
- printk("\n");
- }
- if((i&0xF)!=0)
- printk("\n");
- printk("????????????????????\n");
- break;
- }
- //#endif
- default:
- retval = -ENOTTY;
- break;
- }
- return retval;
- }
- static int I2cOpenFun(struct inode *inode, struct file *pFile)
- {
- // DebugPrint("I2cOpenFun");
- pFile->private_data = (void*) &I2cDev;
- return 0;
- }
- static int I2cCloseFun(struct inode *inode, struct file *pFile)
- {
- // DebugPrint("I2cCloseFun");
- return 0;
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,8)
- static struct file_operations I2cFops = {
- .owner = THIS_MODULE,
- // .read = NsmReadFun,
- // .write = NsmWriteFun,
- .unlocked_ioctl = I2cIoctlFun,
- .open = I2cOpenFun,
- .release = I2cCloseFun,
- };
- #else
- static struct file_operations I2cFops = {
- .owner = THIS_MODULE,
- // .read = NsmReadFun,
- // .write = NsmWriteFun,
- .ioctl = I2cIoctlFun,
- .open = I2cOpenFun,
- .release = I2cCloseFun,
- };
- #endif
- #if 1
- //330 did ot supprt suspend/resume
- #else
- INT32 I2CSuspend(pm_message_t state)
- {
- printk(KERN_EMERG "[I2C]I2CSuspend!!\n");
- UINT32 RegValue;
-
- bSuspend = 1;
- RegValue &= ~MX_CONTROL0_INT_EN;
- MmioWriteFun(M0_CONTROL0,RegValue);
- MmioWriteFun(M1_CONTROL0,RegValue);
- complete(&I2cDev.Comp[0]);
- complete(&I2cDev.Comp[1]);
- //cdev_del(&I2cDev.cdev);
- //destroy_workqueue(I2cDev.i2cWorkQueue);
- cancel_work_sync(&I2cDev.QueueWork);
- //flush_delayed_work(I2cDev.i2cWorkQueue);
- MmioWriteFun(M0_CONTROL0,0x00000000);
- MmioWriteFun(M1_CONTROL0,0x00000000);
- MmioWriteFun(SW_MASTER0,0x000C000C); // disable software mode
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
- //del_timer_sync(&I2cDev.ResetTimer[0]);
- //free_irq(MASTER_IRQ_FINAL,NULL);
- #endif
- return 1;
- }
- INT32 I2CResume(void)
- {
- printk(KERN_EMERG "[I2C]I2CResume!!\n");
- INT32 i,result=0;
- INT32 ret;
- dev_t devno;
- // UINT8 bData;
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
- DebugPrint("I2cInit hardware mode");
- #elif (I2C_MODE==I2C_SOFTWARE_MODE_MASTER)
- DebugPrint("I2cInit software master mode");
- #endif
- memset(&I2cDev,0,sizeof(I2C_DEV));
- for(i=0;i<MASTER_NUMBER;i++)
- {
- sema_init(&I2cDev.I2cMutex[i], 1);
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
- init_completion(&I2cDev.Comp[i]);
- //init_timer(&I2cDev.ResetTimer[i]);
- //I2cDev.ResetTimer[i].data = (UINT32)&I2cDev;
- #endif
- }
- sema_init(&I2cDev.GPIOI2cMutex, 1);
- sema_init(&I2cDev.GPIOXI2cMutex, 1);
- INIT_LIST_HEAD(&I2cDev.pQueueWriteList);
- /*
- I2cDev.i2cWorkQueue = create_singlethread_workqueue("I2C");
- if(!I2cDev.i2cWorkQueue)
- {
- DebugPrint("Cannot create_singlethread_workqueue");
- return -ENOMEM;
- }
- INIT_WORK(&I2cDev.QueueWork,RunQueueFun);
- */
- for(i=0;i<I2C_SPIN_LOCK_MAX;i++)
- {
- spin_lock_init(&I2cDev.Spinlock[i]);
- }
- MmioWriteFun(SW_MASTER0,0x000C000C); // disable software mode
-
- if((readb((void *)SW_MASTER0)&0x30)!=0x30)
- {
- DebugPrint("Error: SDA|SCL=%02x !",readb((void *)SW_MASTER0));
- }
- /*
- devno = MKDEV(I2C_DEV_MAJOR, 0); // I2C_DEV_MAJOR=91
- cdev_init(&I2cDev.cdev, &I2cFops);
- I2cDev.cdev.owner = THIS_MODULE;
-
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
- set_vi_handler(MASTER_IRQ_FINAL,i2c_dispatch);
- result = request_irq(MASTER_IRQ_FINAL,MasterI2cInterruptFun,0,"master_i2c",NULL);
- if(result)
- {
- DebugPrint("request_irq fault");
- return result;
- }
- #endif
- */
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
- #if ONE_BYTE_INTERRUPT
- MmioWriteFun(M0_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN|MX_CONTROL0_ONE_BYTE_INT);
- MmioWriteFun(M1_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN|MX_CONTROL0_ONE_BYTE_INT);
- #else
- MmioWriteFun(M0_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN);
- MmioWriteFun(M1_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN);
- #endif
- MasterAsyncResetFun(0,I2C_SPEED_100K); // Asynchronous reset
- MasterAsyncResetFun(1,I2C_SPEED_100K); // Asynchronous reset
- DebugPrint("Info[0]=0x%08x, Info[1]=0x%08x",(UINT32)&I2cDev.IntInfo[0],(UINT32)&I2cDev.IntInfo[1]);
- #endif
- bSuspend = 0;
- /*
- result = cdev_add(&I2cDev.cdev, devno, 1);
- if(result)
- {
- DebugPrint("cdev_add fault");
- return result;
- }
- */
- return result;
- }
- #endif
- dev_t devno;
- INT32 __init I2cInit(void)
- {
- INT32 i,result=0;
- // dev_t devno;
- // UINT8 bData;
- #if CONFIG_CHIPID == 0x330
- UINT32 value;
- #endif
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
- DebugPrint("I2cInit hardware mode");
- #elif (I2C_MODE==I2C_SOFTWARE_MODE_MASTER)
- DebugPrint("I2cInit software master mode");
- #endif
- /* // Hardware reset
- bData = mReadByteFun(0xBE00000C);
- bData |= 0x04;
- mWriteByteFun(0xBE00000C,bData);
- udelay(1);
- bData &= ~0x04;
- mWriteByteFun(0xBE00000C,bData);
- */
- memset(&I2cDev,0,sizeof(I2C_DEV));
- for(i=0;i<MASTER_NUMBER;i++)
- {
- sema_init(&I2cDev.I2cMutex[i], 1);
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
- init_completion(&I2cDev.Comp[i]);
- //init_timer(&I2cDev.ResetTimer[i]);
- //I2cDev.ResetTimer[i].data = (UINT32)&I2cDev;
- #endif
- }
- sema_init(&I2cDev.GPIOI2cMutex, 1);
- sema_init(&I2cDev.GPIOXI2cMutex, 1);
-
- /*#if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
- I2cDev.ResetTimer[0].function = i2cM0TimeOutFun;
- I2cDev.ResetTimer[1].function = i2cM1TimeOutFun;
- #endif*/
- INIT_LIST_HEAD(&I2cDev.pQueueWriteList);
- INIT_LIST_HEAD(&CmdList);
- I2cDev.i2cWorkQueue = create_singlethread_workqueue("I2C");
- if(!I2cDev.i2cWorkQueue)
- {
- DebugPrint("Cannot create_singlethread_workqueue");
- return -ENOMEM;
- }
- INIT_WORK(&I2cDev.QueueWork,RunQueueFun);
- for(i=0;i<I2C_SPIN_LOCK_MAX;i++)
- {
- spin_lock_init(&I2cDev.Spinlock[i]);
- }
- MmioWriteFun(SW_MASTER0,0x000C000C); // disable software mode
- if((readb((void *)SW_MASTER0)&0x30)!=0x30)
- {
- printk("%s:%d SW_MASTER0 : SDA|SCL=%02x !",__FUNCTION__,__LINE__,readb((void *)SW_MASTER0));
- }
- devno = MKDEV(I2C_DEV_MAJOR, 0); // I2C_DEV_MAJOR=91
- result = register_chrdev_region(devno, 1, "I2C"); //20110807 lwhite add
- if(result)
- {
- DebugPrint("register_chrdev_region fault");
- return result;
- }
-
- cdev_init(&I2cDev.cdev, &I2cFops);
- I2cDev.cdev.owner = THIS_MODULE;
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
- set_vi_handler(MASTER_A_IRQ_FINAL,i2c_dispatch_A);
- result = request_irq(MASTER_A_IRQ_FINAL,MasterAI2cInterruptFun,/*SA_INTERRUPT*/0,"master_i2c",NULL);
- if(result)
- {
- DebugPrint("request_irq fault");
- return result;
- }
- set_vi_handler(MASTER_B_IRQ_FINAL,i2c_dispatch_B);
- result = request_irq(MASTER_B_IRQ_FINAL,MasterBI2cInterruptFun,/*SA_INTERRUPT*/0,"master_i2c",NULL);
- if(result)
- {
- DebugPrint("request_irq fault");
- return result;
- }
- #endif
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
- #if ONE_BYTE_INTERRUPT
- 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);
- 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);
- #else
- MmioWriteFun(M0_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
- MmioWriteFun(M1_CONTROL0,MX_CONTROL0_MASTER_EN|MX_CONTROL0_INT_EN|MX_CONTROL0_BUSY_WAIT_EN|MX_CONTROL0_ADDR_INT_DISABLE);
- #endif
- MasterAsyncResetFun(0,I2C_SPEED_100K); // Asynchronous reset
- MasterAsyncResetFun(1,I2C_SPEED_100K); // Asynchronous reset
- DebugPrint("Info[0]=0x%08x, Info[1]=0x%08x",(UINT32)&I2cDev.IntInfo[0],(UINT32)&I2cDev.IntInfo[1]);
- #endif
- result = cdev_add(&I2cDev.cdev, devno, 1);
- if(result)
- {
- DebugPrint("cdev_add fault");
- return result;
- }
- #if 1
- //330 did ot supprt suspend/resume
- #else
- kmf_register_platformfunc(MODULEID_I2C, I2CSuspend, I2CResume);
- #endif
- #if CONFIG_CHIPID == 0x330
- //Enable the second I2C master
- value = readl((UINT32*)0xbe0f0604); //open gpio 21,22
- value = (value & 0xffffc3ff) | 0x00002800;
- writel(value, (UINT32*)0xbe0f0604);
- value = readl((UINT32*)0xbe0f0620);
- // value = (value & 0xff9fffff) | 0x00600000;//Enable internal pull-up
- value = (value & 0xff9fffff) ;//Disable internal pull-up
- writel(value, (UINT32*)0xbe0f0620);
- #endif
- if((readb((void *)SW_MASTER1)&0x30)!=0x30)
- {
- printk("%s:%d SW_MASTER1 : SDA|SCL=%02x !",__FUNCTION__,__LINE__,readb((void *)SW_MASTER1));
- }
- return result;
- }
- void __exit I2cExit(void)
- {
- // struct delayed_work delaywork;
- //struct work_struct *work;
- //struct work_struct QueueWork;
- //PI2C_DEV pI2cDev = container_of(work, I2C_DEV, QueueWork);
- //PI2C_DEV pI2cDev = &I2cDev;
- DebugPrint("I2cExit");
- cdev_del(&I2cDev.cdev);
- unregister_chrdev_region(devno, 1); //20110807 lwhite add
- /*
- cancel_delayed_work(&pI2cDev->QueueWork);
- flush_workqueue(pI2cDev->i2cWorkQueue);
- destroy_workqueue(pI2cDev->i2cWorkQueue);
- */
- // struct work_struct *testWork;
- // cancel_delayed_work(&testWork);
- // INIT_DELAYED_WORK(&delaywork,RunQueueFun);
- // cancel_delayed_work(&delaywork);
- // flush_workqueue(I2cDev.i2cWorkQueue);
- destroy_workqueue(I2cDev.i2cWorkQueue);
- MmioWriteFun(M0_CONTROL0,0x00000000);
- MmioWriteFun(M1_CONTROL0,0x00000000);
- MmioWriteFun(SW_MASTER0,0x000C000C); // disable software mode
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER) && INTERRUPT_ENABLE
- /*INT32 i;
- for(i=0;i<MASTER_NUMBER;i++)
- {
- del_timer_sync(&I2cDev.ResetTimer[i]);
- }*/
- free_irq(MASTER_A_IRQ_FINAL,NULL);
- free_irq(MASTER_B_IRQ_FINAL,NULL);
- #endif
- }
- #ifndef INIT_BY_KMF
- module_init (I2cInit);
- module_exit (I2cExit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Cheng-Hung Wang");
- #endif
- #if SOFTWARE_MODE_MASTER
- //EXPORT_SYMBOL(SwI2CWriteBasicFun);
- //EXPORT_SYMBOL(SwI2CReadBasicFun);
- //EXPORT_SYMBOL(SwI2CWriteMultiBytesFun);
- EXPORT_SYMBOL(DRV_SwI2C_ReadFun);
- //EXPORT_SYMBOL(SwI2CReadMultiBytesTempFun); //20081110 lwhite test
- #endif
- #if (I2C_MODE==I2C_HARDWARE_MODE_MASTER)
- EXPORT_SYMBOL(DRV_I2C_ReadFun);
- EXPORT_SYMBOL(DRV_I2C_WriteFun);
- EXPORT_SYMBOL(DRV_I2C_M0BasicReadFun);
- EXPORT_SYMBOL(DRV_I2C_M1BasicReadFun);
- EXPORT_SYMBOL(DRV_I2C_M0BasicWriteFun);
- EXPORT_SYMBOL(DRV_I2C_M1BasicWriteFun);
- EXPORT_SYMBOL(DRV_I2C_QueueCmdFun);
- EXPORT_SYMBOL(DRV_I2C_RunCmdFun);
- #endif
|