ptp_ocp.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2020 Facebook */
  3. #include <linux/bits.h>
  4. #include <linux/err.h>
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/init.h>
  9. #include <linux/pci.h>
  10. #include <linux/serial_8250.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/platform_data/i2c-xiic.h>
  15. #include <linux/platform_data/i2c-ocores.h>
  16. #include <linux/ptp_clock_kernel.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/spi/xilinx_spi.h>
  19. #include <linux/spi/altera.h>
  20. #include <net/devlink.h>
  21. #include <linux/i2c.h>
  22. #include <linux/mtd/mtd.h>
  23. #include <linux/nvmem-consumer.h>
  24. #include <linux/crc16.h>
  25. #include <linux/dpll.h>
  26. #define PCI_DEVICE_ID_META_TIMECARD 0x0400
  27. #define PCI_VENDOR_ID_CELESTICA 0x18d4
  28. #define PCI_DEVICE_ID_CELESTICA_TIMECARD 0x1008
  29. #define PCI_VENDOR_ID_OROLIA 0x1ad7
  30. #define PCI_DEVICE_ID_OROLIA_ARTCARD 0xa000
  31. #define PCI_VENDOR_ID_ADVA 0xad5a
  32. #define PCI_DEVICE_ID_ADVA_TIMECARD 0x0400
  33. static struct class timecard_class = {
  34. .name = "timecard",
  35. };
  36. struct ocp_reg {
  37. u32 ctrl;
  38. u32 status;
  39. u32 select;
  40. u32 version;
  41. u32 time_ns;
  42. u32 time_sec;
  43. u32 __pad0[2];
  44. u32 adjust_ns;
  45. u32 adjust_sec;
  46. u32 __pad1[2];
  47. u32 offset_ns;
  48. u32 offset_window_ns;
  49. u32 __pad2[2];
  50. u32 drift_ns;
  51. u32 drift_window_ns;
  52. u32 __pad3[6];
  53. u32 servo_offset_p;
  54. u32 servo_offset_i;
  55. u32 servo_drift_p;
  56. u32 servo_drift_i;
  57. u32 status_offset;
  58. u32 status_drift;
  59. };
  60. struct ptp_ocp_servo_conf {
  61. u32 servo_offset_p;
  62. u32 servo_offset_i;
  63. u32 servo_drift_p;
  64. u32 servo_drift_i;
  65. };
  66. #define OCP_CTRL_ENABLE BIT(0)
  67. #define OCP_CTRL_ADJUST_TIME BIT(1)
  68. #define OCP_CTRL_ADJUST_OFFSET BIT(2)
  69. #define OCP_CTRL_ADJUST_DRIFT BIT(3)
  70. #define OCP_CTRL_ADJUST_SERVO BIT(8)
  71. #define OCP_CTRL_READ_TIME_REQ BIT(30)
  72. #define OCP_CTRL_READ_TIME_DONE BIT(31)
  73. #define OCP_STATUS_IN_SYNC BIT(0)
  74. #define OCP_STATUS_IN_HOLDOVER BIT(1)
  75. #define OCP_SELECT_CLK_NONE 0
  76. #define OCP_SELECT_CLK_REG 0xfe
  77. struct tod_reg {
  78. u32 ctrl;
  79. u32 status;
  80. u32 uart_polarity;
  81. u32 version;
  82. u32 adj_sec;
  83. u32 __pad0[3];
  84. u32 uart_baud;
  85. u32 __pad1[3];
  86. u32 utc_status;
  87. u32 leap;
  88. };
  89. #define TOD_CTRL_PROTOCOL BIT(28)
  90. #define TOD_CTRL_DISABLE_FMT_A BIT(17)
  91. #define TOD_CTRL_DISABLE_FMT_B BIT(16)
  92. #define TOD_CTRL_ENABLE BIT(0)
  93. #define TOD_CTRL_GNSS_MASK GENMASK(3, 0)
  94. #define TOD_CTRL_GNSS_SHIFT 24
  95. #define TOD_STATUS_UTC_MASK GENMASK(7, 0)
  96. #define TOD_STATUS_UTC_VALID BIT(8)
  97. #define TOD_STATUS_LEAP_ANNOUNCE BIT(12)
  98. #define TOD_STATUS_LEAP_VALID BIT(16)
  99. struct ts_reg {
  100. u32 enable;
  101. u32 error;
  102. u32 polarity;
  103. u32 version;
  104. u32 __pad0[4];
  105. u32 cable_delay;
  106. u32 __pad1[3];
  107. u32 intr;
  108. u32 intr_mask;
  109. u32 event_count;
  110. u32 __pad2[1];
  111. u32 ts_count;
  112. u32 time_ns;
  113. u32 time_sec;
  114. u32 data_width;
  115. u32 data;
  116. };
  117. struct pps_reg {
  118. u32 ctrl;
  119. u32 status;
  120. u32 __pad0[6];
  121. u32 cable_delay;
  122. };
  123. #define PPS_STATUS_FILTER_ERR BIT(0)
  124. #define PPS_STATUS_SUPERV_ERR BIT(1)
  125. struct img_reg {
  126. u32 version;
  127. };
  128. struct gpio_reg {
  129. u32 gpio1;
  130. u32 __pad0;
  131. u32 gpio2;
  132. u32 __pad1;
  133. };
  134. struct irig_master_reg {
  135. u32 ctrl;
  136. u32 status;
  137. u32 __pad0;
  138. u32 version;
  139. u32 adj_sec;
  140. u32 mode_ctrl;
  141. };
  142. #define IRIG_M_CTRL_ENABLE BIT(0)
  143. struct irig_slave_reg {
  144. u32 ctrl;
  145. u32 status;
  146. u32 __pad0;
  147. u32 version;
  148. u32 adj_sec;
  149. u32 mode_ctrl;
  150. };
  151. #define IRIG_S_CTRL_ENABLE BIT(0)
  152. struct dcf_master_reg {
  153. u32 ctrl;
  154. u32 status;
  155. u32 __pad0;
  156. u32 version;
  157. u32 adj_sec;
  158. };
  159. #define DCF_M_CTRL_ENABLE BIT(0)
  160. struct dcf_slave_reg {
  161. u32 ctrl;
  162. u32 status;
  163. u32 __pad0;
  164. u32 version;
  165. u32 adj_sec;
  166. };
  167. #define DCF_S_CTRL_ENABLE BIT(0)
  168. struct signal_reg {
  169. u32 enable;
  170. u32 status;
  171. u32 polarity;
  172. u32 version;
  173. u32 __pad0[4];
  174. u32 cable_delay;
  175. u32 __pad1[3];
  176. u32 intr;
  177. u32 intr_mask;
  178. u32 __pad2[2];
  179. u32 start_ns;
  180. u32 start_sec;
  181. u32 pulse_ns;
  182. u32 pulse_sec;
  183. u32 period_ns;
  184. u32 period_sec;
  185. u32 repeat_count;
  186. };
  187. struct frequency_reg {
  188. u32 ctrl;
  189. u32 status;
  190. };
  191. struct board_config_reg {
  192. u32 mro50_serial_activate;
  193. };
  194. #define FREQ_STATUS_VALID BIT(31)
  195. #define FREQ_STATUS_ERROR BIT(30)
  196. #define FREQ_STATUS_OVERRUN BIT(29)
  197. #define FREQ_STATUS_MASK GENMASK(23, 0)
  198. struct ptp_ocp_flash_info {
  199. const char *name;
  200. int pci_offset;
  201. int data_size;
  202. void *data;
  203. };
  204. struct ptp_ocp_firmware_header {
  205. char magic[4];
  206. __be16 pci_vendor_id;
  207. __be16 pci_device_id;
  208. __be32 image_size;
  209. __be16 hw_revision;
  210. __be16 crc;
  211. };
  212. #define OCP_FIRMWARE_MAGIC_HEADER "OCPC"
  213. struct ptp_ocp_i2c_info {
  214. const char *name;
  215. unsigned long fixed_rate;
  216. size_t data_size;
  217. void *data;
  218. };
  219. struct ptp_ocp_ext_info {
  220. int index;
  221. irqreturn_t (*irq_fcn)(int irq, void *priv);
  222. int (*enable)(void *priv, u32 req, bool enable);
  223. };
  224. struct ptp_ocp_ext_src {
  225. void __iomem *mem;
  226. struct ptp_ocp *bp;
  227. struct ptp_ocp_ext_info *info;
  228. int irq_vec;
  229. };
  230. enum ptp_ocp_sma_mode {
  231. SMA_MODE_IN,
  232. SMA_MODE_OUT,
  233. };
  234. static struct dpll_pin_frequency ptp_ocp_sma_freq[] = {
  235. DPLL_PIN_FREQUENCY_1PPS,
  236. DPLL_PIN_FREQUENCY_10MHZ,
  237. DPLL_PIN_FREQUENCY_IRIG_B,
  238. DPLL_PIN_FREQUENCY_DCF77,
  239. };
  240. struct ptp_ocp_sma_connector {
  241. enum ptp_ocp_sma_mode mode;
  242. bool fixed_fcn;
  243. bool fixed_dir;
  244. bool disabled;
  245. u8 default_fcn;
  246. struct dpll_pin *dpll_pin;
  247. struct dpll_pin_properties dpll_prop;
  248. dpll_tracker tracker;
  249. };
  250. struct ocp_attr_group {
  251. u64 cap;
  252. const struct attribute_group *group;
  253. };
  254. #define OCP_CAP_BASIC BIT(0)
  255. #define OCP_CAP_SIGNAL BIT(1)
  256. #define OCP_CAP_FREQ BIT(2)
  257. struct ptp_ocp_signal {
  258. ktime_t period;
  259. ktime_t pulse;
  260. ktime_t phase;
  261. ktime_t start;
  262. int duty;
  263. bool polarity;
  264. bool running;
  265. };
  266. struct ptp_ocp_serial_port {
  267. int line;
  268. int baud;
  269. };
  270. #define OCP_BOARD_ID_LEN 13
  271. #define OCP_SERIAL_LEN 6
  272. #define OCP_SMA_NUM 4
  273. #define OCP_SIGNAL_NUM 4
  274. #define OCP_FREQ_NUM 4
  275. enum {
  276. PORT_GNSS,
  277. PORT_GNSS2,
  278. PORT_MAC, /* miniature atomic clock */
  279. PORT_NMEA,
  280. __PORT_COUNT,
  281. };
  282. struct ptp_ocp {
  283. struct pci_dev *pdev;
  284. struct device dev;
  285. spinlock_t lock;
  286. struct ocp_reg __iomem *reg;
  287. struct tod_reg __iomem *tod;
  288. struct pps_reg __iomem *pps_to_ext;
  289. struct pps_reg __iomem *pps_to_clk;
  290. struct board_config_reg __iomem *board_config;
  291. struct gpio_reg __iomem *pps_select;
  292. struct gpio_reg __iomem *sma_map1;
  293. struct gpio_reg __iomem *sma_map2;
  294. struct irig_master_reg __iomem *irig_out;
  295. struct irig_slave_reg __iomem *irig_in;
  296. struct dcf_master_reg __iomem *dcf_out;
  297. struct dcf_slave_reg __iomem *dcf_in;
  298. struct tod_reg __iomem *nmea_out;
  299. struct frequency_reg __iomem *freq_in[OCP_FREQ_NUM];
  300. struct ptp_ocp_ext_src *signal_out[OCP_SIGNAL_NUM];
  301. struct ptp_ocp_ext_src *pps;
  302. struct ptp_ocp_ext_src *ts0;
  303. struct ptp_ocp_ext_src *ts1;
  304. struct ptp_ocp_ext_src *ts2;
  305. struct ptp_ocp_ext_src *ts3;
  306. struct ptp_ocp_ext_src *ts4;
  307. struct ocp_art_gpio_reg __iomem *art_sma;
  308. struct img_reg __iomem *image;
  309. struct ptp_clock *ptp;
  310. struct ptp_clock_info ptp_info;
  311. struct platform_device *i2c_ctrl;
  312. struct platform_device *spi_flash;
  313. struct clk_hw *i2c_clk;
  314. struct timer_list watchdog;
  315. const struct attribute_group **attr_group;
  316. const struct ptp_ocp_eeprom_map *eeprom_map;
  317. struct dentry *debug_root;
  318. bool sync;
  319. time64_t gnss_lost;
  320. struct delayed_work sync_work;
  321. int id;
  322. int n_irqs;
  323. struct ptp_ocp_serial_port port[__PORT_COUNT];
  324. bool fw_loader;
  325. u8 fw_tag;
  326. u16 fw_version;
  327. u8 board_id[OCP_BOARD_ID_LEN];
  328. u8 serial[OCP_SERIAL_LEN];
  329. bool has_eeprom_data;
  330. u32 pps_req_map;
  331. int flash_start;
  332. u32 utc_tai_offset;
  333. u32 ts_window_adjust;
  334. u64 fw_cap;
  335. struct ptp_ocp_signal signal[OCP_SIGNAL_NUM];
  336. struct ptp_ocp_sma_connector sma[OCP_SMA_NUM];
  337. const struct ocp_sma_op *sma_op;
  338. struct dpll_device *dpll;
  339. dpll_tracker tracker;
  340. int signals_nr;
  341. int freq_in_nr;
  342. };
  343. #define OCP_REQ_TIMESTAMP BIT(0)
  344. #define OCP_REQ_PPS BIT(1)
  345. struct ocp_resource {
  346. unsigned long offset;
  347. int size;
  348. int irq_vec;
  349. int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r);
  350. void *extra;
  351. unsigned long bp_offset;
  352. const char * const name;
  353. };
  354. static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r);
  355. static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
  356. static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
  357. static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
  358. static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
  359. static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
  360. static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
  361. static irqreturn_t ptp_ocp_signal_irq(int irq, void *priv);
  362. static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable);
  363. static int ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
  364. struct ptp_perout_request *req);
  365. static int ptp_ocp_signal_enable(void *priv, u32 req, bool enable);
  366. static int ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr);
  367. static int ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
  368. static int ptp_ocp_adva_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
  369. static const struct ocp_attr_group fb_timecard_groups[];
  370. static const struct ocp_attr_group art_timecard_groups[];
  371. static const struct ocp_attr_group adva_timecard_groups[];
  372. struct ptp_ocp_eeprom_map {
  373. u16 off;
  374. u16 len;
  375. u32 bp_offset;
  376. const void * const tag;
  377. };
  378. #define EEPROM_ENTRY(addr, member) \
  379. .off = addr, \
  380. .len = sizeof_field(struct ptp_ocp, member), \
  381. .bp_offset = offsetof(struct ptp_ocp, member)
  382. #define BP_MAP_ENTRY_ADDR(bp, map) ({ \
  383. (void *)((uintptr_t)(bp) + (map)->bp_offset); \
  384. })
  385. static struct ptp_ocp_eeprom_map fb_eeprom_map[] = {
  386. { EEPROM_ENTRY(0x43, board_id) },
  387. { EEPROM_ENTRY(0x00, serial), .tag = "mac" },
  388. { }
  389. };
  390. static struct ptp_ocp_eeprom_map art_eeprom_map[] = {
  391. { EEPROM_ENTRY(0x200 + 0x43, board_id) },
  392. { EEPROM_ENTRY(0x200 + 0x63, serial) },
  393. { }
  394. };
  395. #define bp_assign_entry(bp, res, val) ({ \
  396. uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \
  397. *(typeof(val) *)addr = val; \
  398. })
  399. #define OCP_RES_LOCATION(member) \
  400. .name = #member, .bp_offset = offsetof(struct ptp_ocp, member)
  401. #define OCP_MEM_RESOURCE(member) \
  402. OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem
  403. #define OCP_SERIAL_RESOURCE(member) \
  404. OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial
  405. #define OCP_I2C_RESOURCE(member) \
  406. OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c
  407. #define OCP_SPI_RESOURCE(member) \
  408. OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi
  409. #define OCP_EXT_RESOURCE(member) \
  410. OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext
  411. /* This is the MSI vector mapping used.
  412. * 0: PPS (TS5)
  413. * 1: TS0
  414. * 2: TS1
  415. * 3: GNSS1
  416. * 4: GNSS2
  417. * 5: MAC
  418. * 6: TS2
  419. * 7: I2C controller
  420. * 8: HWICAP (notused)
  421. * 9: SPI Flash
  422. * 10: NMEA
  423. * 11: Signal Generator 1
  424. * 12: Signal Generator 2
  425. * 13: Signal Generator 3
  426. * 14: Signal Generator 4
  427. * 15: TS3
  428. * 16: TS4
  429. --
  430. * 8: Orolia TS1
  431. * 10: Orolia TS2
  432. * 11: Orolia TS0 (GNSS)
  433. * 12: Orolia PPS
  434. * 14: Orolia TS3
  435. * 15: Orolia TS4
  436. */
  437. static struct ocp_resource ocp_fb_resource[] = {
  438. {
  439. OCP_MEM_RESOURCE(reg),
  440. .offset = 0x01000000, .size = 0x10000,
  441. },
  442. {
  443. OCP_EXT_RESOURCE(ts0),
  444. .offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
  445. .extra = &(struct ptp_ocp_ext_info) {
  446. .index = 0,
  447. .irq_fcn = ptp_ocp_ts_irq,
  448. .enable = ptp_ocp_ts_enable,
  449. },
  450. },
  451. {
  452. OCP_EXT_RESOURCE(ts1),
  453. .offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
  454. .extra = &(struct ptp_ocp_ext_info) {
  455. .index = 1,
  456. .irq_fcn = ptp_ocp_ts_irq,
  457. .enable = ptp_ocp_ts_enable,
  458. },
  459. },
  460. {
  461. OCP_EXT_RESOURCE(ts2),
  462. .offset = 0x01060000, .size = 0x10000, .irq_vec = 6,
  463. .extra = &(struct ptp_ocp_ext_info) {
  464. .index = 2,
  465. .irq_fcn = ptp_ocp_ts_irq,
  466. .enable = ptp_ocp_ts_enable,
  467. },
  468. },
  469. {
  470. OCP_EXT_RESOURCE(ts3),
  471. .offset = 0x01110000, .size = 0x10000, .irq_vec = 15,
  472. .extra = &(struct ptp_ocp_ext_info) {
  473. .index = 3,
  474. .irq_fcn = ptp_ocp_ts_irq,
  475. .enable = ptp_ocp_ts_enable,
  476. },
  477. },
  478. {
  479. OCP_EXT_RESOURCE(ts4),
  480. .offset = 0x01120000, .size = 0x10000, .irq_vec = 16,
  481. .extra = &(struct ptp_ocp_ext_info) {
  482. .index = 4,
  483. .irq_fcn = ptp_ocp_ts_irq,
  484. .enable = ptp_ocp_ts_enable,
  485. },
  486. },
  487. /* Timestamp for PHC and/or PPS generator */
  488. {
  489. OCP_EXT_RESOURCE(pps),
  490. .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0,
  491. .extra = &(struct ptp_ocp_ext_info) {
  492. .index = 5,
  493. .irq_fcn = ptp_ocp_ts_irq,
  494. .enable = ptp_ocp_ts_enable,
  495. },
  496. },
  497. {
  498. OCP_EXT_RESOURCE(signal_out[0]),
  499. .offset = 0x010D0000, .size = 0x10000, .irq_vec = 11,
  500. .extra = &(struct ptp_ocp_ext_info) {
  501. .index = 1,
  502. .irq_fcn = ptp_ocp_signal_irq,
  503. .enable = ptp_ocp_signal_enable,
  504. },
  505. },
  506. {
  507. OCP_EXT_RESOURCE(signal_out[1]),
  508. .offset = 0x010E0000, .size = 0x10000, .irq_vec = 12,
  509. .extra = &(struct ptp_ocp_ext_info) {
  510. .index = 2,
  511. .irq_fcn = ptp_ocp_signal_irq,
  512. .enable = ptp_ocp_signal_enable,
  513. },
  514. },
  515. {
  516. OCP_EXT_RESOURCE(signal_out[2]),
  517. .offset = 0x010F0000, .size = 0x10000, .irq_vec = 13,
  518. .extra = &(struct ptp_ocp_ext_info) {
  519. .index = 3,
  520. .irq_fcn = ptp_ocp_signal_irq,
  521. .enable = ptp_ocp_signal_enable,
  522. },
  523. },
  524. {
  525. OCP_EXT_RESOURCE(signal_out[3]),
  526. .offset = 0x01100000, .size = 0x10000, .irq_vec = 14,
  527. .extra = &(struct ptp_ocp_ext_info) {
  528. .index = 4,
  529. .irq_fcn = ptp_ocp_signal_irq,
  530. .enable = ptp_ocp_signal_enable,
  531. },
  532. },
  533. {
  534. OCP_MEM_RESOURCE(pps_to_ext),
  535. .offset = 0x01030000, .size = 0x10000,
  536. },
  537. {
  538. OCP_MEM_RESOURCE(pps_to_clk),
  539. .offset = 0x01040000, .size = 0x10000,
  540. },
  541. {
  542. OCP_MEM_RESOURCE(tod),
  543. .offset = 0x01050000, .size = 0x10000,
  544. },
  545. {
  546. OCP_MEM_RESOURCE(irig_in),
  547. .offset = 0x01070000, .size = 0x10000,
  548. },
  549. {
  550. OCP_MEM_RESOURCE(irig_out),
  551. .offset = 0x01080000, .size = 0x10000,
  552. },
  553. {
  554. OCP_MEM_RESOURCE(dcf_in),
  555. .offset = 0x01090000, .size = 0x10000,
  556. },
  557. {
  558. OCP_MEM_RESOURCE(dcf_out),
  559. .offset = 0x010A0000, .size = 0x10000,
  560. },
  561. {
  562. OCP_MEM_RESOURCE(nmea_out),
  563. .offset = 0x010B0000, .size = 0x10000,
  564. },
  565. {
  566. OCP_MEM_RESOURCE(image),
  567. .offset = 0x00020000, .size = 0x1000,
  568. },
  569. {
  570. OCP_MEM_RESOURCE(pps_select),
  571. .offset = 0x00130000, .size = 0x1000,
  572. },
  573. {
  574. OCP_MEM_RESOURCE(sma_map1),
  575. .offset = 0x00140000, .size = 0x1000,
  576. },
  577. {
  578. OCP_MEM_RESOURCE(sma_map2),
  579. .offset = 0x00220000, .size = 0x1000,
  580. },
  581. {
  582. OCP_I2C_RESOURCE(i2c_ctrl),
  583. .offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
  584. .extra = &(struct ptp_ocp_i2c_info) {
  585. .name = "xiic-i2c",
  586. .fixed_rate = 50000000,
  587. .data_size = sizeof(struct xiic_i2c_platform_data),
  588. .data = &(struct xiic_i2c_platform_data) {
  589. .num_devices = 2,
  590. .devices = (struct i2c_board_info[]) {
  591. { I2C_BOARD_INFO("24c02", 0x50) },
  592. { I2C_BOARD_INFO("24mac402", 0x58),
  593. .platform_data = "mac" },
  594. },
  595. },
  596. },
  597. },
  598. {
  599. OCP_SERIAL_RESOURCE(port[PORT_GNSS]),
  600. .offset = 0x00160000 + 0x1000, .irq_vec = 3,
  601. .extra = &(struct ptp_ocp_serial_port) {
  602. .baud = 115200,
  603. },
  604. },
  605. {
  606. OCP_SERIAL_RESOURCE(port[PORT_GNSS2]),
  607. .offset = 0x00170000 + 0x1000, .irq_vec = 4,
  608. .extra = &(struct ptp_ocp_serial_port) {
  609. .baud = 115200,
  610. },
  611. },
  612. {
  613. OCP_SERIAL_RESOURCE(port[PORT_MAC]),
  614. .offset = 0x00180000 + 0x1000, .irq_vec = 5,
  615. .extra = &(struct ptp_ocp_serial_port) {
  616. .baud = 57600,
  617. },
  618. },
  619. {
  620. OCP_SERIAL_RESOURCE(port[PORT_NMEA]),
  621. .offset = 0x00190000 + 0x1000, .irq_vec = 10,
  622. },
  623. {
  624. OCP_SPI_RESOURCE(spi_flash),
  625. .offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
  626. .extra = &(struct ptp_ocp_flash_info) {
  627. .name = "xilinx_spi", .pci_offset = 0,
  628. .data_size = sizeof(struct xspi_platform_data),
  629. .data = &(struct xspi_platform_data) {
  630. .num_chipselect = 1,
  631. .bits_per_word = 8,
  632. .num_devices = 1,
  633. .force_irq = true,
  634. .devices = &(struct spi_board_info) {
  635. .modalias = "spi-nor",
  636. },
  637. },
  638. },
  639. },
  640. {
  641. OCP_MEM_RESOURCE(freq_in[0]),
  642. .offset = 0x01200000, .size = 0x10000,
  643. },
  644. {
  645. OCP_MEM_RESOURCE(freq_in[1]),
  646. .offset = 0x01210000, .size = 0x10000,
  647. },
  648. {
  649. OCP_MEM_RESOURCE(freq_in[2]),
  650. .offset = 0x01220000, .size = 0x10000,
  651. },
  652. {
  653. OCP_MEM_RESOURCE(freq_in[3]),
  654. .offset = 0x01230000, .size = 0x10000,
  655. },
  656. {
  657. .setup = ptp_ocp_fb_board_init,
  658. .extra = &(struct ptp_ocp_servo_conf) {
  659. .servo_offset_p = 0x2000,
  660. .servo_offset_i = 0x1000,
  661. .servo_drift_p = 0,
  662. .servo_drift_i = 0,
  663. },
  664. },
  665. { }
  666. };
  667. #define OCP_ART_CONFIG_SIZE 144
  668. #define OCP_ART_TEMP_TABLE_SIZE 368
  669. struct ocp_art_gpio_reg {
  670. struct {
  671. u32 gpio;
  672. u32 __pad[3];
  673. } map[4];
  674. };
  675. static struct ocp_resource ocp_art_resource[] = {
  676. {
  677. OCP_MEM_RESOURCE(reg),
  678. .offset = 0x01000000, .size = 0x10000,
  679. },
  680. {
  681. OCP_SERIAL_RESOURCE(port[PORT_GNSS]),
  682. .offset = 0x00160000 + 0x1000, .irq_vec = 3,
  683. .extra = &(struct ptp_ocp_serial_port) {
  684. .baud = 115200,
  685. },
  686. },
  687. {
  688. OCP_MEM_RESOURCE(art_sma),
  689. .offset = 0x003C0000, .size = 0x1000,
  690. },
  691. /* Timestamp associated with GNSS1 receiver PPS */
  692. {
  693. OCP_EXT_RESOURCE(ts0),
  694. .offset = 0x360000, .size = 0x20, .irq_vec = 12,
  695. .extra = &(struct ptp_ocp_ext_info) {
  696. .index = 0,
  697. .irq_fcn = ptp_ocp_ts_irq,
  698. .enable = ptp_ocp_ts_enable,
  699. },
  700. },
  701. {
  702. OCP_EXT_RESOURCE(ts1),
  703. .offset = 0x380000, .size = 0x20, .irq_vec = 8,
  704. .extra = &(struct ptp_ocp_ext_info) {
  705. .index = 1,
  706. .irq_fcn = ptp_ocp_ts_irq,
  707. .enable = ptp_ocp_ts_enable,
  708. },
  709. },
  710. {
  711. OCP_EXT_RESOURCE(ts2),
  712. .offset = 0x390000, .size = 0x20, .irq_vec = 10,
  713. .extra = &(struct ptp_ocp_ext_info) {
  714. .index = 2,
  715. .irq_fcn = ptp_ocp_ts_irq,
  716. .enable = ptp_ocp_ts_enable,
  717. },
  718. },
  719. {
  720. OCP_EXT_RESOURCE(ts3),
  721. .offset = 0x3A0000, .size = 0x20, .irq_vec = 14,
  722. .extra = &(struct ptp_ocp_ext_info) {
  723. .index = 3,
  724. .irq_fcn = ptp_ocp_ts_irq,
  725. .enable = ptp_ocp_ts_enable,
  726. },
  727. },
  728. {
  729. OCP_EXT_RESOURCE(ts4),
  730. .offset = 0x3B0000, .size = 0x20, .irq_vec = 15,
  731. .extra = &(struct ptp_ocp_ext_info) {
  732. .index = 4,
  733. .irq_fcn = ptp_ocp_ts_irq,
  734. .enable = ptp_ocp_ts_enable,
  735. },
  736. },
  737. /* Timestamp associated with Internal PPS of the card */
  738. {
  739. OCP_EXT_RESOURCE(pps),
  740. .offset = 0x00330000, .size = 0x20, .irq_vec = 11,
  741. .extra = &(struct ptp_ocp_ext_info) {
  742. .index = 5,
  743. .irq_fcn = ptp_ocp_ts_irq,
  744. .enable = ptp_ocp_ts_enable,
  745. },
  746. },
  747. {
  748. OCP_SPI_RESOURCE(spi_flash),
  749. .offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
  750. .extra = &(struct ptp_ocp_flash_info) {
  751. .name = "spi_altera", .pci_offset = 0,
  752. .data_size = sizeof(struct altera_spi_platform_data),
  753. .data = &(struct altera_spi_platform_data) {
  754. .num_chipselect = 1,
  755. .num_devices = 1,
  756. .devices = &(struct spi_board_info) {
  757. .modalias = "spi-nor",
  758. },
  759. },
  760. },
  761. },
  762. {
  763. OCP_I2C_RESOURCE(i2c_ctrl),
  764. .offset = 0x350000, .size = 0x100, .irq_vec = 4,
  765. .extra = &(struct ptp_ocp_i2c_info) {
  766. .name = "ocores-i2c",
  767. .fixed_rate = 400000,
  768. .data_size = sizeof(struct ocores_i2c_platform_data),
  769. .data = &(struct ocores_i2c_platform_data) {
  770. .clock_khz = 125000,
  771. .bus_khz = 400,
  772. .num_devices = 1,
  773. .devices = &(struct i2c_board_info) {
  774. I2C_BOARD_INFO("24c08", 0x50),
  775. },
  776. },
  777. },
  778. },
  779. {
  780. OCP_SERIAL_RESOURCE(port[PORT_MAC]),
  781. .offset = 0x00190000, .irq_vec = 7,
  782. .extra = &(struct ptp_ocp_serial_port) {
  783. .baud = 9600,
  784. },
  785. },
  786. {
  787. OCP_MEM_RESOURCE(board_config),
  788. .offset = 0x210000, .size = 0x1000,
  789. },
  790. {
  791. .setup = ptp_ocp_art_board_init,
  792. .extra = &(struct ptp_ocp_servo_conf) {
  793. .servo_offset_p = 0x2000,
  794. .servo_offset_i = 0x1000,
  795. .servo_drift_p = 0,
  796. .servo_drift_i = 0,
  797. },
  798. },
  799. { }
  800. };
  801. static struct ocp_resource ocp_adva_resource[] = {
  802. {
  803. OCP_MEM_RESOURCE(reg),
  804. .offset = 0x01000000, .size = 0x10000,
  805. },
  806. {
  807. OCP_EXT_RESOURCE(ts0),
  808. .offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
  809. .extra = &(struct ptp_ocp_ext_info) {
  810. .index = 0,
  811. .irq_fcn = ptp_ocp_ts_irq,
  812. .enable = ptp_ocp_ts_enable,
  813. },
  814. },
  815. {
  816. OCP_EXT_RESOURCE(ts1),
  817. .offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
  818. .extra = &(struct ptp_ocp_ext_info) {
  819. .index = 1,
  820. .irq_fcn = ptp_ocp_ts_irq,
  821. .enable = ptp_ocp_ts_enable,
  822. },
  823. },
  824. {
  825. OCP_EXT_RESOURCE(ts2),
  826. .offset = 0x01060000, .size = 0x10000, .irq_vec = 6,
  827. .extra = &(struct ptp_ocp_ext_info) {
  828. .index = 2,
  829. .irq_fcn = ptp_ocp_ts_irq,
  830. .enable = ptp_ocp_ts_enable,
  831. },
  832. },
  833. /* Timestamp for PHC and/or PPS generator */
  834. {
  835. OCP_EXT_RESOURCE(pps),
  836. .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0,
  837. .extra = &(struct ptp_ocp_ext_info) {
  838. .index = 5,
  839. .irq_fcn = ptp_ocp_ts_irq,
  840. .enable = ptp_ocp_ts_enable,
  841. },
  842. },
  843. {
  844. OCP_EXT_RESOURCE(signal_out[0]),
  845. .offset = 0x010D0000, .size = 0x10000, .irq_vec = 11,
  846. .extra = &(struct ptp_ocp_ext_info) {
  847. .index = 1,
  848. .irq_fcn = ptp_ocp_signal_irq,
  849. .enable = ptp_ocp_signal_enable,
  850. },
  851. },
  852. {
  853. OCP_EXT_RESOURCE(signal_out[1]),
  854. .offset = 0x010E0000, .size = 0x10000, .irq_vec = 12,
  855. .extra = &(struct ptp_ocp_ext_info) {
  856. .index = 2,
  857. .irq_fcn = ptp_ocp_signal_irq,
  858. .enable = ptp_ocp_signal_enable,
  859. },
  860. },
  861. {
  862. OCP_MEM_RESOURCE(pps_to_ext),
  863. .offset = 0x01030000, .size = 0x10000,
  864. },
  865. {
  866. OCP_MEM_RESOURCE(pps_to_clk),
  867. .offset = 0x01040000, .size = 0x10000,
  868. },
  869. {
  870. OCP_MEM_RESOURCE(tod),
  871. .offset = 0x01050000, .size = 0x10000,
  872. },
  873. {
  874. OCP_MEM_RESOURCE(image),
  875. .offset = 0x00020000, .size = 0x1000,
  876. },
  877. {
  878. OCP_MEM_RESOURCE(pps_select),
  879. .offset = 0x00130000, .size = 0x1000,
  880. },
  881. {
  882. OCP_MEM_RESOURCE(sma_map1),
  883. .offset = 0x00140000, .size = 0x1000,
  884. },
  885. {
  886. OCP_MEM_RESOURCE(sma_map2),
  887. .offset = 0x00220000, .size = 0x1000,
  888. },
  889. {
  890. OCP_SERIAL_RESOURCE(port[PORT_GNSS]),
  891. .offset = 0x00160000 + 0x1000, .irq_vec = 3,
  892. .extra = &(struct ptp_ocp_serial_port) {
  893. .baud = 9600,
  894. },
  895. },
  896. {
  897. OCP_SERIAL_RESOURCE(port[PORT_MAC]),
  898. .offset = 0x00180000 + 0x1000, .irq_vec = 5,
  899. .extra = &(struct ptp_ocp_serial_port) {
  900. .baud = 115200,
  901. },
  902. },
  903. {
  904. OCP_MEM_RESOURCE(freq_in[0]),
  905. .offset = 0x01200000, .size = 0x10000,
  906. },
  907. {
  908. OCP_MEM_RESOURCE(freq_in[1]),
  909. .offset = 0x01210000, .size = 0x10000,
  910. },
  911. {
  912. OCP_SPI_RESOURCE(spi_flash),
  913. .offset = 0x00310400, .size = 0x10000, .irq_vec = 9,
  914. .extra = &(struct ptp_ocp_flash_info) {
  915. .name = "spi_altera", .pci_offset = 0,
  916. .data_size = sizeof(struct altera_spi_platform_data),
  917. .data = &(struct altera_spi_platform_data) {
  918. .num_chipselect = 1,
  919. .num_devices = 1,
  920. .devices = &(struct spi_board_info) {
  921. .modalias = "spi-nor",
  922. },
  923. },
  924. },
  925. },
  926. {
  927. OCP_I2C_RESOURCE(i2c_ctrl),
  928. .offset = 0x150000, .size = 0x100, .irq_vec = 7,
  929. .extra = &(struct ptp_ocp_i2c_info) {
  930. .name = "ocores-i2c",
  931. .fixed_rate = 50000000,
  932. .data_size = sizeof(struct ocores_i2c_platform_data),
  933. .data = &(struct ocores_i2c_platform_data) {
  934. .clock_khz = 50000,
  935. .bus_khz = 100,
  936. .reg_io_width = 4, // 32-bit/4-byte
  937. .reg_shift = 2, // 32-bit addressing
  938. .num_devices = 2,
  939. .devices = (struct i2c_board_info[]) {
  940. { I2C_BOARD_INFO("24c02", 0x50) },
  941. { I2C_BOARD_INFO("24mac402", 0x58),
  942. .platform_data = "mac" },
  943. },
  944. },
  945. },
  946. },
  947. {
  948. .setup = ptp_ocp_adva_board_init,
  949. .extra = &(struct ptp_ocp_servo_conf) {
  950. .servo_offset_p = 0xc000,
  951. .servo_offset_i = 0x1000,
  952. .servo_drift_p = 0,
  953. .servo_drift_i = 0,
  954. },
  955. },
  956. { }
  957. };
  958. static const struct pci_device_id ptp_ocp_pcidev_id[] = {
  959. { PCI_DEVICE_DATA(META, TIMECARD, &ocp_fb_resource) },
  960. { PCI_DEVICE_DATA(CELESTICA, TIMECARD, &ocp_fb_resource) },
  961. { PCI_DEVICE_DATA(OROLIA, ARTCARD, &ocp_art_resource) },
  962. { PCI_DEVICE_DATA(ADVA, TIMECARD, &ocp_adva_resource) },
  963. { }
  964. };
  965. MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id);
  966. static DEFINE_MUTEX(ptp_ocp_lock);
  967. static DEFINE_IDR(ptp_ocp_idr);
  968. struct ocp_selector {
  969. const char *name;
  970. int value;
  971. u64 frequency;
  972. };
  973. static const struct ocp_selector ptp_ocp_clock[] = {
  974. { .name = "NONE", .value = 0 },
  975. { .name = "TOD", .value = 1 },
  976. { .name = "IRIG", .value = 2 },
  977. { .name = "PPS", .value = 3 },
  978. { .name = "PTP", .value = 4 },
  979. { .name = "RTC", .value = 5 },
  980. { .name = "DCF", .value = 6 },
  981. { .name = "REGS", .value = 0xfe },
  982. { .name = "EXT", .value = 0xff },
  983. { }
  984. };
  985. #define SMA_DISABLE BIT(16)
  986. #define SMA_ENABLE BIT(15)
  987. #define SMA_SELECT_MASK GENMASK(14, 0)
  988. static const struct ocp_selector ptp_ocp_sma_in[] = {
  989. { .name = "10Mhz", .value = 0x0000, .frequency = 10000000 },
  990. { .name = "PPS1", .value = 0x0001, .frequency = 1 },
  991. { .name = "PPS2", .value = 0x0002, .frequency = 1 },
  992. { .name = "TS1", .value = 0x0004, .frequency = 0 },
  993. { .name = "TS2", .value = 0x0008, .frequency = 0 },
  994. { .name = "IRIG", .value = 0x0010, .frequency = 10000 },
  995. { .name = "DCF", .value = 0x0020, .frequency = 77500 },
  996. { .name = "TS3", .value = 0x0040, .frequency = 0 },
  997. { .name = "TS4", .value = 0x0080, .frequency = 0 },
  998. { .name = "FREQ1", .value = 0x0100, .frequency = 0 },
  999. { .name = "FREQ2", .value = 0x0200, .frequency = 0 },
  1000. { .name = "FREQ3", .value = 0x0400, .frequency = 0 },
  1001. { .name = "FREQ4", .value = 0x0800, .frequency = 0 },
  1002. { .name = "None", .value = SMA_DISABLE, .frequency = 0 },
  1003. { }
  1004. };
  1005. static const struct ocp_selector ptp_ocp_sma_out[] = {
  1006. { .name = "10Mhz", .value = 0x0000, .frequency = 10000000 },
  1007. { .name = "PHC", .value = 0x0001, .frequency = 1 },
  1008. { .name = "MAC", .value = 0x0002, .frequency = 1 },
  1009. { .name = "GNSS1", .value = 0x0004, .frequency = 1 },
  1010. { .name = "GNSS2", .value = 0x0008, .frequency = 1 },
  1011. { .name = "IRIG", .value = 0x0010, .frequency = 10000 },
  1012. { .name = "DCF", .value = 0x0020, .frequency = 77000 },
  1013. { .name = "GEN1", .value = 0x0040 },
  1014. { .name = "GEN2", .value = 0x0080 },
  1015. { .name = "GEN3", .value = 0x0100 },
  1016. { .name = "GEN4", .value = 0x0200 },
  1017. { .name = "GND", .value = 0x2000 },
  1018. { .name = "VCC", .value = 0x4000 },
  1019. { }
  1020. };
  1021. static const struct ocp_selector ptp_ocp_art_sma_in[] = {
  1022. { .name = "PPS1", .value = 0x0001, .frequency = 1 },
  1023. { .name = "10Mhz", .value = 0x0008, .frequency = 1000000 },
  1024. { }
  1025. };
  1026. static const struct ocp_selector ptp_ocp_art_sma_out[] = {
  1027. { .name = "PHC", .value = 0x0002, .frequency = 1 },
  1028. { .name = "GNSS", .value = 0x0004, .frequency = 1 },
  1029. { .name = "10Mhz", .value = 0x0010, .frequency = 10000000 },
  1030. { }
  1031. };
  1032. static const struct ocp_selector ptp_ocp_adva_sma_in[] = {
  1033. { .name = "10Mhz", .value = 0x0000, .frequency = 10000000},
  1034. { .name = "PPS1", .value = 0x0001, .frequency = 1 },
  1035. { .name = "PPS2", .value = 0x0002, .frequency = 1 },
  1036. { .name = "TS1", .value = 0x0004, .frequency = 0 },
  1037. { .name = "TS2", .value = 0x0008, .frequency = 0 },
  1038. { .name = "FREQ1", .value = 0x0100, .frequency = 0 },
  1039. { .name = "FREQ2", .value = 0x0200, .frequency = 0 },
  1040. { .name = "None", .value = SMA_DISABLE, .frequency = 0 },
  1041. { }
  1042. };
  1043. static const struct ocp_selector ptp_ocp_adva_sma_out[] = {
  1044. { .name = "10Mhz", .value = 0x0000, .frequency = 10000000},
  1045. { .name = "PHC", .value = 0x0001, .frequency = 1 },
  1046. { .name = "MAC", .value = 0x0002, .frequency = 1 },
  1047. { .name = "GNSS1", .value = 0x0004, .frequency = 1 },
  1048. { .name = "GEN1", .value = 0x0040 },
  1049. { .name = "GEN2", .value = 0x0080 },
  1050. { .name = "GND", .value = 0x2000 },
  1051. { .name = "VCC", .value = 0x4000 },
  1052. { }
  1053. };
  1054. struct ocp_sma_op {
  1055. const struct ocp_selector *tbl[2];
  1056. void (*init)(struct ptp_ocp *bp);
  1057. u32 (*get)(struct ptp_ocp *bp, int sma_nr);
  1058. int (*set_inputs)(struct ptp_ocp *bp, int sma_nr, u32 val);
  1059. int (*set_output)(struct ptp_ocp *bp, int sma_nr, u32 val);
  1060. };
  1061. static void
  1062. ptp_ocp_sma_init(struct ptp_ocp *bp)
  1063. {
  1064. return bp->sma_op->init(bp);
  1065. }
  1066. static u32
  1067. ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr)
  1068. {
  1069. return bp->sma_op->get(bp, sma_nr);
  1070. }
  1071. static int
  1072. ptp_ocp_sma_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
  1073. {
  1074. return bp->sma_op->set_inputs(bp, sma_nr, val);
  1075. }
  1076. static int
  1077. ptp_ocp_sma_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
  1078. {
  1079. return bp->sma_op->set_output(bp, sma_nr, val);
  1080. }
  1081. static const char *
  1082. ptp_ocp_select_name_from_val(const struct ocp_selector *tbl, int val)
  1083. {
  1084. int i;
  1085. for (i = 0; tbl[i].name; i++)
  1086. if (tbl[i].value == val)
  1087. return tbl[i].name;
  1088. return NULL;
  1089. }
  1090. static int
  1091. ptp_ocp_select_val_from_name(const struct ocp_selector *tbl, const char *name)
  1092. {
  1093. const char *select;
  1094. int i;
  1095. for (i = 0; tbl[i].name; i++) {
  1096. select = tbl[i].name;
  1097. if (!strncasecmp(name, select, strlen(select)))
  1098. return tbl[i].value;
  1099. }
  1100. return -EINVAL;
  1101. }
  1102. static ssize_t
  1103. ptp_ocp_select_table_show(const struct ocp_selector *tbl, char *buf)
  1104. {
  1105. ssize_t count;
  1106. int i;
  1107. count = 0;
  1108. for (i = 0; tbl[i].name; i++)
  1109. count += sysfs_emit_at(buf, count, "%s ", tbl[i].name);
  1110. if (count)
  1111. count--;
  1112. count += sysfs_emit_at(buf, count, "\n");
  1113. return count;
  1114. }
  1115. static int
  1116. __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
  1117. struct ptp_system_timestamp *sts)
  1118. {
  1119. u32 ctrl, time_sec, time_ns;
  1120. int i;
  1121. ptp_read_system_prets(sts);
  1122. ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
  1123. iowrite32(ctrl, &bp->reg->ctrl);
  1124. for (i = 0; i < 100; i++) {
  1125. ctrl = ioread32(&bp->reg->ctrl);
  1126. if (ctrl & OCP_CTRL_READ_TIME_DONE)
  1127. break;
  1128. }
  1129. ptp_read_system_postts(sts);
  1130. if (sts && bp->ts_window_adjust) {
  1131. s64 ns = timespec64_to_ns(&sts->post_ts);
  1132. sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust);
  1133. }
  1134. time_ns = ioread32(&bp->reg->time_ns);
  1135. time_sec = ioread32(&bp->reg->time_sec);
  1136. ts->tv_sec = time_sec;
  1137. ts->tv_nsec = time_ns;
  1138. return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT;
  1139. }
  1140. static int
  1141. ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
  1142. struct ptp_system_timestamp *sts)
  1143. {
  1144. struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
  1145. unsigned long flags;
  1146. int err;
  1147. spin_lock_irqsave(&bp->lock, flags);
  1148. err = __ptp_ocp_gettime_locked(bp, ts, sts);
  1149. spin_unlock_irqrestore(&bp->lock, flags);
  1150. return err;
  1151. }
  1152. static void
  1153. __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
  1154. {
  1155. u32 ctrl, time_sec, time_ns;
  1156. u32 select;
  1157. time_ns = ts->tv_nsec;
  1158. time_sec = ts->tv_sec;
  1159. select = ioread32(&bp->reg->select);
  1160. iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
  1161. iowrite32(time_ns, &bp->reg->adjust_ns);
  1162. iowrite32(time_sec, &bp->reg->adjust_sec);
  1163. ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE;
  1164. iowrite32(ctrl, &bp->reg->ctrl);
  1165. /* restore clock selection */
  1166. iowrite32(select >> 16, &bp->reg->select);
  1167. }
  1168. static int
  1169. ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
  1170. {
  1171. struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
  1172. unsigned long flags;
  1173. spin_lock_irqsave(&bp->lock, flags);
  1174. __ptp_ocp_settime_locked(bp, ts);
  1175. spin_unlock_irqrestore(&bp->lock, flags);
  1176. return 0;
  1177. }
  1178. static void
  1179. __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val)
  1180. {
  1181. u32 select, ctrl;
  1182. select = ioread32(&bp->reg->select);
  1183. iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
  1184. iowrite32(adj_val, &bp->reg->offset_ns);
  1185. iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns);
  1186. ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE;
  1187. iowrite32(ctrl, &bp->reg->ctrl);
  1188. /* restore clock selection */
  1189. iowrite32(select >> 16, &bp->reg->select);
  1190. }
  1191. static void
  1192. ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, s64 delta_ns)
  1193. {
  1194. struct timespec64 ts;
  1195. unsigned long flags;
  1196. int err;
  1197. spin_lock_irqsave(&bp->lock, flags);
  1198. err = __ptp_ocp_gettime_locked(bp, &ts, NULL);
  1199. if (likely(!err)) {
  1200. set_normalized_timespec64(&ts, ts.tv_sec,
  1201. ts.tv_nsec + delta_ns);
  1202. __ptp_ocp_settime_locked(bp, &ts);
  1203. }
  1204. spin_unlock_irqrestore(&bp->lock, flags);
  1205. }
  1206. static int
  1207. ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns)
  1208. {
  1209. struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
  1210. unsigned long flags;
  1211. u32 adj_ns, sign;
  1212. if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) {
  1213. ptp_ocp_adjtime_coarse(bp, delta_ns);
  1214. return 0;
  1215. }
  1216. sign = delta_ns < 0 ? BIT(31) : 0;
  1217. adj_ns = sign ? -delta_ns : delta_ns;
  1218. spin_lock_irqsave(&bp->lock, flags);
  1219. __ptp_ocp_adjtime_locked(bp, sign | adj_ns);
  1220. spin_unlock_irqrestore(&bp->lock, flags);
  1221. return 0;
  1222. }
  1223. static int
  1224. ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
  1225. {
  1226. if (scaled_ppm == 0)
  1227. return 0;
  1228. return -EOPNOTSUPP;
  1229. }
  1230. static s32
  1231. ptp_ocp_null_getmaxphase(struct ptp_clock_info *ptp_info)
  1232. {
  1233. return 0;
  1234. }
  1235. static int
  1236. ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
  1237. {
  1238. return -EOPNOTSUPP;
  1239. }
  1240. static int
  1241. ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
  1242. int on)
  1243. {
  1244. struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
  1245. struct ptp_ocp_ext_src *ext = NULL;
  1246. u32 req;
  1247. int err;
  1248. switch (rq->type) {
  1249. case PTP_CLK_REQ_EXTTS:
  1250. req = OCP_REQ_TIMESTAMP;
  1251. switch (rq->extts.index) {
  1252. case 0:
  1253. ext = bp->ts0;
  1254. break;
  1255. case 1:
  1256. ext = bp->ts1;
  1257. break;
  1258. case 2:
  1259. ext = bp->ts2;
  1260. break;
  1261. case 3:
  1262. ext = bp->ts3;
  1263. break;
  1264. case 4:
  1265. ext = bp->ts4;
  1266. break;
  1267. case 5:
  1268. ext = bp->pps;
  1269. break;
  1270. }
  1271. break;
  1272. case PTP_CLK_REQ_PPS:
  1273. req = OCP_REQ_PPS;
  1274. ext = bp->pps;
  1275. break;
  1276. case PTP_CLK_REQ_PEROUT:
  1277. switch (rq->perout.index) {
  1278. case 0:
  1279. /* This is a request for 1PPS on an output SMA.
  1280. * Allow, but assume manual configuration.
  1281. */
  1282. if (on && (rq->perout.period.sec != 1 ||
  1283. rq->perout.period.nsec != 0))
  1284. return -EINVAL;
  1285. return 0;
  1286. case 1:
  1287. case 2:
  1288. case 3:
  1289. case 4:
  1290. req = rq->perout.index - 1;
  1291. ext = bp->signal_out[req];
  1292. err = ptp_ocp_signal_from_perout(bp, req, &rq->perout);
  1293. if (err)
  1294. return err;
  1295. break;
  1296. }
  1297. break;
  1298. default:
  1299. return -EOPNOTSUPP;
  1300. }
  1301. err = -ENXIO;
  1302. if (ext)
  1303. err = ext->info->enable(ext, req, on);
  1304. return err;
  1305. }
  1306. static int
  1307. ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin,
  1308. enum ptp_pin_function func, unsigned chan)
  1309. {
  1310. struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
  1311. char buf[16];
  1312. switch (func) {
  1313. case PTP_PF_NONE:
  1314. snprintf(buf, sizeof(buf), "IN: None");
  1315. break;
  1316. case PTP_PF_EXTTS:
  1317. /* Allow timestamps, but require sysfs configuration. */
  1318. return 0;
  1319. case PTP_PF_PEROUT:
  1320. /* channel 0 is 1PPS from PHC.
  1321. * channels 1..4 are the frequency generators.
  1322. */
  1323. if (chan)
  1324. snprintf(buf, sizeof(buf), "OUT: GEN%d", chan);
  1325. else
  1326. snprintf(buf, sizeof(buf), "OUT: PHC");
  1327. break;
  1328. default:
  1329. return -EOPNOTSUPP;
  1330. }
  1331. return ptp_ocp_sma_store(bp, buf, pin + 1);
  1332. }
  1333. static const struct ptp_clock_info ptp_ocp_clock_info = {
  1334. .owner = THIS_MODULE,
  1335. .name = KBUILD_MODNAME,
  1336. .max_adj = 100000000,
  1337. .gettimex64 = ptp_ocp_gettimex,
  1338. .settime64 = ptp_ocp_settime,
  1339. .adjtime = ptp_ocp_adjtime,
  1340. .adjfine = ptp_ocp_null_adjfine,
  1341. .adjphase = ptp_ocp_null_adjphase,
  1342. .getmaxphase = ptp_ocp_null_getmaxphase,
  1343. .enable = ptp_ocp_enable,
  1344. .verify = ptp_ocp_verify,
  1345. .pps = true,
  1346. .n_ext_ts = 6,
  1347. .n_per_out = 5,
  1348. .supported_extts_flags = PTP_STRICT_FLAGS | PTP_RISING_EDGE,
  1349. .supported_perout_flags = PTP_PEROUT_DUTY_CYCLE | PTP_PEROUT_PHASE,
  1350. };
  1351. static void
  1352. __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
  1353. {
  1354. u32 ctrl, select;
  1355. select = ioread32(&bp->reg->select);
  1356. iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
  1357. iowrite32(0, &bp->reg->drift_ns);
  1358. ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE;
  1359. iowrite32(ctrl, &bp->reg->ctrl);
  1360. /* restore clock selection */
  1361. iowrite32(select >> 16, &bp->reg->select);
  1362. }
  1363. static void
  1364. ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val)
  1365. {
  1366. unsigned long flags;
  1367. spin_lock_irqsave(&bp->lock, flags);
  1368. bp->utc_tai_offset = val;
  1369. if (bp->irig_out)
  1370. iowrite32(val, &bp->irig_out->adj_sec);
  1371. if (bp->dcf_out)
  1372. iowrite32(val, &bp->dcf_out->adj_sec);
  1373. if (bp->nmea_out)
  1374. iowrite32(val, &bp->nmea_out->adj_sec);
  1375. spin_unlock_irqrestore(&bp->lock, flags);
  1376. }
  1377. static void
  1378. ptp_ocp_watchdog(struct timer_list *t)
  1379. {
  1380. struct ptp_ocp *bp = timer_container_of(bp, t, watchdog);
  1381. unsigned long flags;
  1382. u32 status, utc_offset;
  1383. status = ioread32(&bp->pps_to_clk->status);
  1384. if (status & PPS_STATUS_SUPERV_ERR) {
  1385. iowrite32(status, &bp->pps_to_clk->status);
  1386. if (!bp->gnss_lost) {
  1387. spin_lock_irqsave(&bp->lock, flags);
  1388. __ptp_ocp_clear_drift_locked(bp);
  1389. spin_unlock_irqrestore(&bp->lock, flags);
  1390. bp->gnss_lost = ktime_get_real_seconds();
  1391. }
  1392. } else if (bp->gnss_lost) {
  1393. bp->gnss_lost = 0;
  1394. }
  1395. /* if GNSS provides correct data we can rely on
  1396. * it to get leap second information
  1397. */
  1398. if (bp->tod) {
  1399. status = ioread32(&bp->tod->utc_status);
  1400. utc_offset = status & TOD_STATUS_UTC_MASK;
  1401. if (status & TOD_STATUS_UTC_VALID &&
  1402. utc_offset != bp->utc_tai_offset)
  1403. ptp_ocp_utc_distribute(bp, utc_offset);
  1404. }
  1405. mod_timer(&bp->watchdog, jiffies + HZ);
  1406. }
  1407. static void
  1408. ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp)
  1409. {
  1410. ktime_t start, end, delay = U64_MAX;
  1411. u32 ctrl;
  1412. int i;
  1413. for (i = 0; i < 3; i++) {
  1414. ctrl = ioread32(&bp->reg->ctrl);
  1415. ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
  1416. iowrite32(ctrl, &bp->reg->ctrl);
  1417. start = ktime_get_raw_ns();
  1418. ctrl = ioread32(&bp->reg->ctrl);
  1419. end = ktime_get_raw_ns();
  1420. delay = min(delay, end - start);
  1421. }
  1422. bp->ts_window_adjust = (delay >> 5) * 3;
  1423. }
  1424. static int
  1425. ptp_ocp_init_clock(struct ptp_ocp *bp, struct ptp_ocp_servo_conf *servo_conf)
  1426. {
  1427. struct timespec64 ts;
  1428. u32 ctrl;
  1429. ctrl = OCP_CTRL_ENABLE;
  1430. iowrite32(ctrl, &bp->reg->ctrl);
  1431. /* servo configuration */
  1432. iowrite32(servo_conf->servo_offset_p, &bp->reg->servo_offset_p);
  1433. iowrite32(servo_conf->servo_offset_i, &bp->reg->servo_offset_i);
  1434. iowrite32(servo_conf->servo_drift_p, &bp->reg->servo_drift_p);
  1435. iowrite32(servo_conf->servo_drift_p, &bp->reg->servo_drift_i);
  1436. /* latch servo values */
  1437. ctrl |= OCP_CTRL_ADJUST_SERVO;
  1438. iowrite32(ctrl, &bp->reg->ctrl);
  1439. if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
  1440. dev_err(&bp->pdev->dev, "clock not enabled\n");
  1441. return -ENODEV;
  1442. }
  1443. ptp_ocp_estimate_pci_timing(bp);
  1444. bp->sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
  1445. if (!bp->sync) {
  1446. ktime_get_clocktai_ts64(&ts);
  1447. ptp_ocp_settime(&bp->ptp_info, &ts);
  1448. }
  1449. /* If there is a clock supervisor, then enable the watchdog */
  1450. if (bp->pps_to_clk) {
  1451. timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
  1452. mod_timer(&bp->watchdog, jiffies + HZ);
  1453. }
  1454. return 0;
  1455. }
  1456. static void
  1457. ptp_ocp_tod_init(struct ptp_ocp *bp)
  1458. {
  1459. u32 ctrl, reg;
  1460. ctrl = ioread32(&bp->tod->ctrl);
  1461. ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
  1462. ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
  1463. iowrite32(ctrl, &bp->tod->ctrl);
  1464. reg = ioread32(&bp->tod->utc_status);
  1465. if (reg & TOD_STATUS_UTC_VALID)
  1466. ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK);
  1467. }
  1468. static const char *
  1469. ptp_ocp_tod_proto_name(const int idx)
  1470. {
  1471. static const char * const proto_name[] = {
  1472. "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
  1473. "UBX", "UBX_UTC", "UBX_LS", "UBX_none"
  1474. };
  1475. return proto_name[idx];
  1476. }
  1477. static const char *
  1478. ptp_ocp_tod_gnss_name(int idx)
  1479. {
  1480. static const char * const gnss_name[] = {
  1481. "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
  1482. "Unknown"
  1483. };
  1484. if (idx >= ARRAY_SIZE(gnss_name))
  1485. idx = ARRAY_SIZE(gnss_name) - 1;
  1486. return gnss_name[idx];
  1487. }
  1488. static const char *
  1489. ptp_ocp_tty_port_name(int idx)
  1490. {
  1491. static const char * const tty_name[] = {
  1492. "GNSS", "GNSS2", "MAC", "NMEA"
  1493. };
  1494. return tty_name[idx];
  1495. }
  1496. struct ptp_ocp_nvmem_match_info {
  1497. struct ptp_ocp *bp;
  1498. const void * const tag;
  1499. };
  1500. static int
  1501. ptp_ocp_nvmem_match(struct device *dev, const void *data)
  1502. {
  1503. const struct ptp_ocp_nvmem_match_info *info = data;
  1504. dev = dev->parent;
  1505. if (!i2c_verify_client(dev) || info->tag != dev->platform_data)
  1506. return 0;
  1507. while ((dev = dev->parent))
  1508. if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
  1509. return info->bp == dev_get_drvdata(dev);
  1510. return 0;
  1511. }
  1512. static inline struct nvmem_device *
  1513. ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag)
  1514. {
  1515. struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag };
  1516. return nvmem_device_find(&info, ptp_ocp_nvmem_match);
  1517. }
  1518. static inline void
  1519. ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp)
  1520. {
  1521. if (!IS_ERR_OR_NULL(*nvmemp))
  1522. nvmem_device_put(*nvmemp);
  1523. *nvmemp = NULL;
  1524. }
  1525. static void
  1526. ptp_ocp_read_eeprom(struct ptp_ocp *bp)
  1527. {
  1528. const struct ptp_ocp_eeprom_map *map;
  1529. struct nvmem_device *nvmem;
  1530. const void *tag;
  1531. int ret;
  1532. if (!bp->i2c_ctrl)
  1533. return;
  1534. tag = NULL;
  1535. nvmem = NULL;
  1536. for (map = bp->eeprom_map; map->len; map++) {
  1537. if (map->tag != tag) {
  1538. tag = map->tag;
  1539. ptp_ocp_nvmem_device_put(&nvmem);
  1540. }
  1541. if (!nvmem) {
  1542. nvmem = ptp_ocp_nvmem_device_get(bp, tag);
  1543. if (IS_ERR(nvmem)) {
  1544. ret = PTR_ERR(nvmem);
  1545. goto fail;
  1546. }
  1547. }
  1548. ret = nvmem_device_read(nvmem, map->off, map->len,
  1549. BP_MAP_ENTRY_ADDR(bp, map));
  1550. if (ret != map->len)
  1551. goto fail;
  1552. }
  1553. bp->has_eeprom_data = true;
  1554. out:
  1555. ptp_ocp_nvmem_device_put(&nvmem);
  1556. return;
  1557. fail:
  1558. dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret);
  1559. goto out;
  1560. }
  1561. static struct device *
  1562. ptp_ocp_find_flash(struct ptp_ocp *bp)
  1563. {
  1564. struct device *dev, *last;
  1565. last = NULL;
  1566. dev = &bp->spi_flash->dev;
  1567. while ((dev = device_find_any_child(dev))) {
  1568. if (!strcmp("mtd", dev_bus_name(dev)))
  1569. break;
  1570. put_device(last);
  1571. last = dev;
  1572. }
  1573. put_device(last);
  1574. return dev;
  1575. }
  1576. static int
  1577. ptp_ocp_devlink_fw_image(struct devlink *devlink, const struct firmware *fw,
  1578. const u8 **data, size_t *size)
  1579. {
  1580. struct ptp_ocp *bp = devlink_priv(devlink);
  1581. const struct ptp_ocp_firmware_header *hdr;
  1582. size_t offset, length;
  1583. u16 crc;
  1584. hdr = (const struct ptp_ocp_firmware_header *)fw->data;
  1585. if (memcmp(hdr->magic, OCP_FIRMWARE_MAGIC_HEADER, 4)) {
  1586. devlink_flash_update_status_notify(devlink,
  1587. "No firmware header found, cancel firmware upgrade",
  1588. NULL, 0, 0);
  1589. return -EINVAL;
  1590. }
  1591. if (be16_to_cpu(hdr->pci_vendor_id) != bp->pdev->vendor ||
  1592. be16_to_cpu(hdr->pci_device_id) != bp->pdev->device) {
  1593. devlink_flash_update_status_notify(devlink,
  1594. "Firmware image compatibility check failed",
  1595. NULL, 0, 0);
  1596. return -EINVAL;
  1597. }
  1598. offset = sizeof(*hdr);
  1599. length = be32_to_cpu(hdr->image_size);
  1600. if (length != (fw->size - offset)) {
  1601. devlink_flash_update_status_notify(devlink,
  1602. "Firmware image size check failed",
  1603. NULL, 0, 0);
  1604. return -EINVAL;
  1605. }
  1606. crc = crc16(0xffff, &fw->data[offset], length);
  1607. if (be16_to_cpu(hdr->crc) != crc) {
  1608. devlink_flash_update_status_notify(devlink,
  1609. "Firmware image CRC check failed",
  1610. NULL, 0, 0);
  1611. return -EINVAL;
  1612. }
  1613. *data = &fw->data[offset];
  1614. *size = length;
  1615. return 0;
  1616. }
  1617. static int
  1618. ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
  1619. const struct firmware *fw)
  1620. {
  1621. struct mtd_info *mtd = dev_get_drvdata(dev);
  1622. struct ptp_ocp *bp = devlink_priv(devlink);
  1623. size_t off, len, size, resid, wrote;
  1624. struct erase_info erase;
  1625. size_t base, blksz;
  1626. const u8 *data;
  1627. int err;
  1628. err = ptp_ocp_devlink_fw_image(devlink, fw, &data, &size);
  1629. if (err)
  1630. goto out;
  1631. off = 0;
  1632. base = bp->flash_start;
  1633. blksz = 4096;
  1634. resid = size;
  1635. while (resid) {
  1636. devlink_flash_update_status_notify(devlink, "Flashing",
  1637. NULL, off, size);
  1638. len = min_t(size_t, resid, blksz);
  1639. erase.addr = base + off;
  1640. erase.len = blksz;
  1641. err = mtd_erase(mtd, &erase);
  1642. if (err)
  1643. goto out;
  1644. err = mtd_write(mtd, base + off, len, &wrote, data + off);
  1645. if (err)
  1646. goto out;
  1647. off += blksz;
  1648. resid -= len;
  1649. }
  1650. out:
  1651. return err;
  1652. }
  1653. static int
  1654. ptp_ocp_devlink_flash_update(struct devlink *devlink,
  1655. struct devlink_flash_update_params *params,
  1656. struct netlink_ext_ack *extack)
  1657. {
  1658. struct ptp_ocp *bp = devlink_priv(devlink);
  1659. struct device *dev;
  1660. const char *msg;
  1661. int err;
  1662. dev = ptp_ocp_find_flash(bp);
  1663. if (!dev) {
  1664. dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
  1665. return -ENODEV;
  1666. }
  1667. devlink_flash_update_status_notify(devlink, "Preparing to flash",
  1668. NULL, 0, 0);
  1669. err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
  1670. msg = err ? "Flash error" : "Flash complete";
  1671. devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
  1672. put_device(dev);
  1673. return err;
  1674. }
  1675. static int
  1676. ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
  1677. struct netlink_ext_ack *extack)
  1678. {
  1679. struct ptp_ocp *bp = devlink_priv(devlink);
  1680. const char *fw_image;
  1681. char buf[32];
  1682. int err;
  1683. fw_image = bp->fw_loader ? "loader" : "fw";
  1684. sprintf(buf, "%d.%d", bp->fw_tag, bp->fw_version);
  1685. err = devlink_info_version_running_put(req, fw_image, buf);
  1686. if (err)
  1687. return err;
  1688. if (!bp->has_eeprom_data) {
  1689. ptp_ocp_read_eeprom(bp);
  1690. if (!bp->has_eeprom_data)
  1691. return 0;
  1692. }
  1693. sprintf(buf, "%pM", bp->serial);
  1694. err = devlink_info_serial_number_put(req, buf);
  1695. if (err)
  1696. return err;
  1697. err = devlink_info_version_fixed_put(req,
  1698. DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
  1699. bp->board_id);
  1700. if (err)
  1701. return err;
  1702. return 0;
  1703. }
  1704. static const struct devlink_ops ptp_ocp_devlink_ops = {
  1705. .flash_update = ptp_ocp_devlink_flash_update,
  1706. .info_get = ptp_ocp_devlink_info_get,
  1707. };
  1708. static void __iomem *
  1709. __ptp_ocp_get_mem(struct ptp_ocp *bp, resource_size_t start, int size)
  1710. {
  1711. struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
  1712. return devm_ioremap_resource(&bp->pdev->dev, &res);
  1713. }
  1714. static void __iomem *
  1715. ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
  1716. {
  1717. resource_size_t start;
  1718. start = pci_resource_start(bp->pdev, 0) + r->offset;
  1719. return __ptp_ocp_get_mem(bp, start, r->size);
  1720. }
  1721. static int
  1722. ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
  1723. {
  1724. struct ptp_ocp_flash_info *info;
  1725. struct pci_dev *pdev = bp->pdev;
  1726. struct platform_device *p;
  1727. struct resource res[2];
  1728. resource_size_t start;
  1729. int id;
  1730. start = pci_resource_start(pdev, 0) + r->offset;
  1731. res[0] = DEFINE_RES_MEM(start, r->size);
  1732. res[1] = DEFINE_RES_IRQ(pci_irq_vector(pdev, r->irq_vec));
  1733. info = r->extra;
  1734. id = pci_dev_id(pdev) << 1;
  1735. id += info->pci_offset;
  1736. p = platform_device_register_resndata(&pdev->dev, info->name, id,
  1737. res, ARRAY_SIZE(res), info->data,
  1738. info->data_size);
  1739. if (IS_ERR(p))
  1740. return PTR_ERR(p);
  1741. bp_assign_entry(bp, r, p);
  1742. return 0;
  1743. }
  1744. static struct platform_device *
  1745. ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
  1746. {
  1747. struct ptp_ocp_i2c_info *info;
  1748. struct resource res[2];
  1749. resource_size_t start;
  1750. info = r->extra;
  1751. start = pci_resource_start(pdev, 0) + r->offset;
  1752. res[0] = DEFINE_RES_MEM(start, r->size);
  1753. res[1] = DEFINE_RES_IRQ(pci_irq_vector(pdev, r->irq_vec));
  1754. return platform_device_register_resndata(&pdev->dev, info->name,
  1755. id, res, ARRAY_SIZE(res),
  1756. info->data, info->data_size);
  1757. }
  1758. static int
  1759. ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
  1760. {
  1761. struct pci_dev *pdev = bp->pdev;
  1762. struct ptp_ocp_i2c_info *info;
  1763. struct platform_device *p;
  1764. struct clk_hw *clk;
  1765. char buf[32];
  1766. int id;
  1767. info = r->extra;
  1768. id = pci_dev_id(bp->pdev);
  1769. sprintf(buf, "AXI.%d", id);
  1770. clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0,
  1771. info->fixed_rate);
  1772. if (IS_ERR(clk))
  1773. return PTR_ERR(clk);
  1774. bp->i2c_clk = clk;
  1775. sprintf(buf, "%s.%d", info->name, id);
  1776. devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
  1777. p = ptp_ocp_i2c_bus(bp->pdev, r, id);
  1778. if (IS_ERR(p))
  1779. return PTR_ERR(p);
  1780. bp_assign_entry(bp, r, p);
  1781. return 0;
  1782. }
  1783. /* The expectation is that this is triggered only on error. */
  1784. static irqreturn_t
  1785. ptp_ocp_signal_irq(int irq, void *priv)
  1786. {
  1787. struct ptp_ocp_ext_src *ext = priv;
  1788. struct signal_reg __iomem *reg = ext->mem;
  1789. struct ptp_ocp *bp = ext->bp;
  1790. u32 enable, status;
  1791. int gen;
  1792. gen = ext->info->index - 1;
  1793. enable = ioread32(&reg->enable);
  1794. status = ioread32(&reg->status);
  1795. /* disable generator on error */
  1796. if (status || !enable) {
  1797. iowrite32(0, &reg->intr_mask);
  1798. iowrite32(0, &reg->enable);
  1799. bp->signal[gen].running = false;
  1800. }
  1801. iowrite32(0, &reg->intr); /* ack interrupt */
  1802. return IRQ_HANDLED;
  1803. }
  1804. static int
  1805. ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s)
  1806. {
  1807. struct ptp_system_timestamp sts;
  1808. struct timespec64 ts;
  1809. ktime_t start_ns;
  1810. int err;
  1811. if (!s->period)
  1812. return 0;
  1813. if (!s->pulse)
  1814. s->pulse = ktime_divns(s->period * s->duty, 100);
  1815. err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts);
  1816. if (err)
  1817. return err;
  1818. start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC;
  1819. if (!s->start) {
  1820. /* roundup() does not work on 32-bit systems */
  1821. s->start = DIV64_U64_ROUND_UP(start_ns, s->period);
  1822. s->start *= s->period;
  1823. s->start = ktime_add(s->start, s->phase);
  1824. }
  1825. if (s->duty < 1 || s->duty > 99)
  1826. return -EINVAL;
  1827. if (s->pulse < 1 || s->pulse > s->period)
  1828. return -EINVAL;
  1829. if (s->start < start_ns)
  1830. return -EINVAL;
  1831. bp->signal[gen] = *s;
  1832. return 0;
  1833. }
  1834. static int
  1835. ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
  1836. struct ptp_perout_request *req)
  1837. {
  1838. struct ptp_ocp_signal s = { };
  1839. s.polarity = bp->signal[gen].polarity;
  1840. s.period = ktime_set(req->period.sec, req->period.nsec);
  1841. if (!s.period)
  1842. return 0;
  1843. if (req->flags & PTP_PEROUT_DUTY_CYCLE) {
  1844. s.pulse = ktime_set(req->on.sec, req->on.nsec);
  1845. s.duty = ktime_divns(s.pulse * 100, s.period);
  1846. }
  1847. if (req->flags & PTP_PEROUT_PHASE)
  1848. s.phase = ktime_set(req->phase.sec, req->phase.nsec);
  1849. else
  1850. s.start = ktime_set(req->start.sec, req->start.nsec);
  1851. return ptp_ocp_signal_set(bp, gen, &s);
  1852. }
  1853. static int
  1854. ptp_ocp_signal_enable(void *priv, u32 req, bool enable)
  1855. {
  1856. struct ptp_ocp_ext_src *ext = priv;
  1857. struct signal_reg __iomem *reg = ext->mem;
  1858. struct ptp_ocp *bp = ext->bp;
  1859. struct timespec64 ts;
  1860. int gen;
  1861. gen = ext->info->index - 1;
  1862. iowrite32(0, &reg->intr_mask);
  1863. iowrite32(0, &reg->enable);
  1864. bp->signal[gen].running = false;
  1865. if (!enable)
  1866. return 0;
  1867. ts = ktime_to_timespec64(bp->signal[gen].start);
  1868. iowrite32(ts.tv_sec, &reg->start_sec);
  1869. iowrite32(ts.tv_nsec, &reg->start_ns);
  1870. ts = ktime_to_timespec64(bp->signal[gen].period);
  1871. iowrite32(ts.tv_sec, &reg->period_sec);
  1872. iowrite32(ts.tv_nsec, &reg->period_ns);
  1873. ts = ktime_to_timespec64(bp->signal[gen].pulse);
  1874. iowrite32(ts.tv_sec, &reg->pulse_sec);
  1875. iowrite32(ts.tv_nsec, &reg->pulse_ns);
  1876. iowrite32(bp->signal[gen].polarity, &reg->polarity);
  1877. iowrite32(0, &reg->repeat_count);
  1878. iowrite32(0, &reg->intr); /* clear interrupt state */
  1879. iowrite32(1, &reg->intr_mask); /* enable interrupt */
  1880. iowrite32(3, &reg->enable); /* valid & enable */
  1881. bp->signal[gen].running = true;
  1882. return 0;
  1883. }
  1884. static irqreturn_t
  1885. ptp_ocp_ts_irq(int irq, void *priv)
  1886. {
  1887. struct ptp_ocp_ext_src *ext = priv;
  1888. struct ts_reg __iomem *reg = ext->mem;
  1889. struct ptp_clock_event ev;
  1890. u32 sec, nsec;
  1891. if (ext == ext->bp->pps) {
  1892. if (ext->bp->pps_req_map & OCP_REQ_PPS) {
  1893. ev.type = PTP_CLOCK_PPS;
  1894. ptp_clock_event(ext->bp->ptp, &ev);
  1895. }
  1896. if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0)
  1897. goto out;
  1898. }
  1899. /* XXX should fix API - this converts s/ns -> ts -> s/ns */
  1900. sec = ioread32(&reg->time_sec);
  1901. nsec = ioread32(&reg->time_ns);
  1902. ev.type = PTP_CLOCK_EXTTS;
  1903. ev.index = ext->info->index;
  1904. ev.timestamp = sec * NSEC_PER_SEC + nsec;
  1905. ptp_clock_event(ext->bp->ptp, &ev);
  1906. out:
  1907. iowrite32(1, &reg->intr); /* write 1 to ack */
  1908. return IRQ_HANDLED;
  1909. }
  1910. static int
  1911. ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
  1912. {
  1913. struct ptp_ocp_ext_src *ext = priv;
  1914. struct ts_reg __iomem *reg = ext->mem;
  1915. struct ptp_ocp *bp = ext->bp;
  1916. if (ext == bp->pps) {
  1917. u32 old_map = bp->pps_req_map;
  1918. if (enable)
  1919. bp->pps_req_map |= req;
  1920. else
  1921. bp->pps_req_map &= ~req;
  1922. /* if no state change, just return */
  1923. if ((!!old_map ^ !!bp->pps_req_map) == 0)
  1924. return 0;
  1925. }
  1926. if (enable) {
  1927. iowrite32(1, &reg->enable);
  1928. iowrite32(1, &reg->intr_mask);
  1929. iowrite32(1, &reg->intr);
  1930. } else {
  1931. iowrite32(0, &reg->intr_mask);
  1932. iowrite32(0, &reg->enable);
  1933. }
  1934. return 0;
  1935. }
  1936. static void
  1937. ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
  1938. {
  1939. if (!ext)
  1940. return;
  1941. ext->info->enable(ext, ~0, false);
  1942. pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
  1943. kfree(ext);
  1944. }
  1945. static int
  1946. ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
  1947. {
  1948. struct pci_dev *pdev = bp->pdev;
  1949. struct ptp_ocp_ext_src *ext;
  1950. int err;
  1951. ext = kzalloc_obj(*ext);
  1952. if (!ext)
  1953. return -ENOMEM;
  1954. ext->mem = ptp_ocp_get_mem(bp, r);
  1955. if (IS_ERR(ext->mem)) {
  1956. err = PTR_ERR(ext->mem);
  1957. goto out;
  1958. }
  1959. ext->bp = bp;
  1960. ext->info = r->extra;
  1961. ext->irq_vec = r->irq_vec;
  1962. err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
  1963. ext, "ocp%d.%s", bp->id, r->name);
  1964. if (err) {
  1965. dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
  1966. goto out;
  1967. }
  1968. bp_assign_entry(bp, r, ext);
  1969. return 0;
  1970. out:
  1971. kfree(ext);
  1972. return err;
  1973. }
  1974. static int
  1975. ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
  1976. {
  1977. struct pci_dev *pdev = bp->pdev;
  1978. struct uart_8250_port uart;
  1979. /* Setting UPF_IOREMAP and leaving port.membase unspecified lets
  1980. * the serial port device claim and release the pci resource.
  1981. */
  1982. memset(&uart, 0, sizeof(uart));
  1983. uart.port.dev = &pdev->dev;
  1984. uart.port.iotype = UPIO_MEM;
  1985. uart.port.regshift = 2;
  1986. uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
  1987. uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
  1988. uart.port.uartclk = 50000000;
  1989. uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST;
  1990. uart.port.type = PORT_16550A;
  1991. return serial8250_register_8250_port(&uart);
  1992. }
  1993. static int
  1994. ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
  1995. {
  1996. struct ptp_ocp_serial_port *p = (struct ptp_ocp_serial_port *)r->extra;
  1997. struct ptp_ocp_serial_port port = {};
  1998. port.line = ptp_ocp_serial_line(bp, r);
  1999. if (port.line < 0)
  2000. return port.line;
  2001. if (p)
  2002. port.baud = p->baud;
  2003. bp_assign_entry(bp, r, port);
  2004. return 0;
  2005. }
  2006. static int
  2007. ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
  2008. {
  2009. void __iomem *mem;
  2010. mem = ptp_ocp_get_mem(bp, r);
  2011. if (IS_ERR(mem))
  2012. return PTR_ERR(mem);
  2013. bp_assign_entry(bp, r, mem);
  2014. return 0;
  2015. }
  2016. static void
  2017. ptp_ocp_nmea_out_init(struct ptp_ocp *bp)
  2018. {
  2019. if (!bp->nmea_out)
  2020. return;
  2021. iowrite32(0, &bp->nmea_out->ctrl); /* disable */
  2022. iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */
  2023. iowrite32(1, &bp->nmea_out->ctrl); /* enable */
  2024. }
  2025. static void
  2026. _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg)
  2027. {
  2028. u32 val;
  2029. iowrite32(0, &reg->enable); /* disable */
  2030. val = ioread32(&reg->polarity);
  2031. s->polarity = val ? true : false;
  2032. s->duty = 50;
  2033. }
  2034. static void
  2035. ptp_ocp_signal_init(struct ptp_ocp *bp)
  2036. {
  2037. int i;
  2038. for (i = 0; i < 4; i++)
  2039. if (bp->signal_out[i])
  2040. _ptp_ocp_signal_init(&bp->signal[i],
  2041. bp->signal_out[i]->mem);
  2042. }
  2043. static void
  2044. ptp_ocp_attr_group_del(struct ptp_ocp *bp)
  2045. {
  2046. sysfs_remove_groups(&bp->dev.kobj, bp->attr_group);
  2047. kfree(bp->attr_group);
  2048. }
  2049. static int
  2050. ptp_ocp_attr_group_add(struct ptp_ocp *bp,
  2051. const struct ocp_attr_group *attr_tbl)
  2052. {
  2053. int count, i;
  2054. int err;
  2055. count = 0;
  2056. for (i = 0; attr_tbl[i].cap; i++)
  2057. if (attr_tbl[i].cap & bp->fw_cap)
  2058. count++;
  2059. bp->attr_group = kzalloc_objs(*bp->attr_group, count + 1);
  2060. if (!bp->attr_group)
  2061. return -ENOMEM;
  2062. count = 0;
  2063. for (i = 0; attr_tbl[i].cap; i++)
  2064. if (attr_tbl[i].cap & bp->fw_cap)
  2065. bp->attr_group[count++] = attr_tbl[i].group;
  2066. err = sysfs_create_groups(&bp->dev.kobj, bp->attr_group);
  2067. if (err)
  2068. bp->attr_group[0] = NULL;
  2069. return err;
  2070. }
  2071. static void
  2072. ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable)
  2073. {
  2074. u32 ctrl;
  2075. bool on;
  2076. ctrl = ioread32(reg);
  2077. on = ctrl & bit;
  2078. if (on ^ enable) {
  2079. ctrl &= ~bit;
  2080. ctrl |= enable ? bit : 0;
  2081. iowrite32(ctrl, reg);
  2082. }
  2083. }
  2084. static void
  2085. ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable)
  2086. {
  2087. return ptp_ocp_enable_fpga(&bp->irig_out->ctrl,
  2088. IRIG_M_CTRL_ENABLE, enable);
  2089. }
  2090. static void
  2091. ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable)
  2092. {
  2093. return ptp_ocp_enable_fpga(&bp->irig_in->ctrl,
  2094. IRIG_S_CTRL_ENABLE, enable);
  2095. }
  2096. static void
  2097. ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable)
  2098. {
  2099. return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl,
  2100. DCF_M_CTRL_ENABLE, enable);
  2101. }
  2102. static void
  2103. ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable)
  2104. {
  2105. return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl,
  2106. DCF_S_CTRL_ENABLE, enable);
  2107. }
  2108. static void
  2109. __handle_signal_outputs(struct ptp_ocp *bp, u32 val)
  2110. {
  2111. ptp_ocp_irig_out(bp, val & 0x00100010);
  2112. ptp_ocp_dcf_out(bp, val & 0x00200020);
  2113. }
  2114. static void
  2115. __handle_signal_inputs(struct ptp_ocp *bp, u32 val)
  2116. {
  2117. ptp_ocp_irig_in(bp, val & 0x00100010);
  2118. ptp_ocp_dcf_in(bp, val & 0x00200020);
  2119. }
  2120. static u32
  2121. ptp_ocp_sma_fb_get(struct ptp_ocp *bp, int sma_nr)
  2122. {
  2123. u32 __iomem *gpio;
  2124. u32 shift;
  2125. if (bp->sma[sma_nr - 1].fixed_fcn)
  2126. return (sma_nr - 1) & 1;
  2127. if (bp->sma[sma_nr - 1].mode == SMA_MODE_IN)
  2128. gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
  2129. else
  2130. gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
  2131. shift = sma_nr & 1 ? 0 : 16;
  2132. return (ioread32(gpio) >> shift) & 0xffff;
  2133. }
  2134. static int
  2135. ptp_ocp_sma_fb_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
  2136. {
  2137. u32 reg, mask, shift;
  2138. unsigned long flags;
  2139. u32 __iomem *gpio;
  2140. gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
  2141. shift = sma_nr & 1 ? 0 : 16;
  2142. mask = 0xffff << (16 - shift);
  2143. spin_lock_irqsave(&bp->lock, flags);
  2144. reg = ioread32(gpio);
  2145. reg = (reg & mask) | (val << shift);
  2146. __handle_signal_outputs(bp, reg);
  2147. iowrite32(reg, gpio);
  2148. spin_unlock_irqrestore(&bp->lock, flags);
  2149. return 0;
  2150. }
  2151. static int
  2152. ptp_ocp_sma_fb_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
  2153. {
  2154. u32 reg, mask, shift;
  2155. unsigned long flags;
  2156. u32 __iomem *gpio;
  2157. gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
  2158. shift = sma_nr & 1 ? 0 : 16;
  2159. mask = 0xffff << (16 - shift);
  2160. spin_lock_irqsave(&bp->lock, flags);
  2161. reg = ioread32(gpio);
  2162. reg = (reg & mask) | (val << shift);
  2163. __handle_signal_inputs(bp, reg);
  2164. iowrite32(reg, gpio);
  2165. spin_unlock_irqrestore(&bp->lock, flags);
  2166. return 0;
  2167. }
  2168. static void
  2169. ptp_ocp_sma_fb_init(struct ptp_ocp *bp)
  2170. {
  2171. struct dpll_pin_properties prop = {
  2172. .board_label = NULL,
  2173. .type = DPLL_PIN_TYPE_EXT,
  2174. .capabilities = DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE,
  2175. .freq_supported_num = ARRAY_SIZE(ptp_ocp_sma_freq),
  2176. .freq_supported = ptp_ocp_sma_freq,
  2177. };
  2178. u32 reg;
  2179. int i;
  2180. /* defaults */
  2181. for (i = 0; i < OCP_SMA_NUM; i++) {
  2182. bp->sma[i].default_fcn = i & 1;
  2183. bp->sma[i].dpll_prop = prop;
  2184. bp->sma[i].dpll_prop.board_label =
  2185. bp->ptp_info.pin_config[i].name;
  2186. }
  2187. bp->sma[0].mode = SMA_MODE_IN;
  2188. bp->sma[1].mode = SMA_MODE_IN;
  2189. bp->sma[2].mode = SMA_MODE_OUT;
  2190. bp->sma[3].mode = SMA_MODE_OUT;
  2191. /* If no SMA1 map, the pin functions and directions are fixed. */
  2192. if (!bp->sma_map1) {
  2193. for (i = 0; i < OCP_SMA_NUM; i++) {
  2194. bp->sma[i].fixed_fcn = true;
  2195. bp->sma[i].fixed_dir = true;
  2196. bp->sma[i].dpll_prop.capabilities &=
  2197. ~DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE;
  2198. }
  2199. return;
  2200. }
  2201. /* If SMA2 GPIO output map is all 1, it is not present.
  2202. * This indicates the firmware has fixed direction SMA pins.
  2203. */
  2204. reg = ioread32(&bp->sma_map2->gpio2);
  2205. if (reg == 0xffffffff) {
  2206. for (i = 0; i < OCP_SMA_NUM; i++)
  2207. bp->sma[i].fixed_dir = true;
  2208. } else {
  2209. reg = ioread32(&bp->sma_map1->gpio1);
  2210. bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT;
  2211. bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT;
  2212. reg = ioread32(&bp->sma_map1->gpio2);
  2213. bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN;
  2214. bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN;
  2215. }
  2216. }
  2217. static const struct ocp_sma_op ocp_fb_sma_op = {
  2218. .tbl = { ptp_ocp_sma_in, ptp_ocp_sma_out },
  2219. .init = ptp_ocp_sma_fb_init,
  2220. .get = ptp_ocp_sma_fb_get,
  2221. .set_inputs = ptp_ocp_sma_fb_set_inputs,
  2222. .set_output = ptp_ocp_sma_fb_set_output,
  2223. };
  2224. static int
  2225. ptp_ocp_sma_adva_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
  2226. {
  2227. u32 reg, mask, shift;
  2228. unsigned long flags;
  2229. u32 __iomem *gpio;
  2230. gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
  2231. shift = sma_nr & 1 ? 0 : 16;
  2232. mask = 0xffff << (16 - shift);
  2233. spin_lock_irqsave(&bp->lock, flags);
  2234. reg = ioread32(gpio);
  2235. reg = (reg & mask) | (val << shift);
  2236. iowrite32(reg, gpio);
  2237. spin_unlock_irqrestore(&bp->lock, flags);
  2238. return 0;
  2239. }
  2240. static int
  2241. ptp_ocp_sma_adva_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
  2242. {
  2243. u32 reg, mask, shift;
  2244. unsigned long flags;
  2245. u32 __iomem *gpio;
  2246. gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
  2247. shift = sma_nr & 1 ? 0 : 16;
  2248. mask = 0xffff << (16 - shift);
  2249. spin_lock_irqsave(&bp->lock, flags);
  2250. reg = ioread32(gpio);
  2251. reg = (reg & mask) | (val << shift);
  2252. iowrite32(reg, gpio);
  2253. spin_unlock_irqrestore(&bp->lock, flags);
  2254. return 0;
  2255. }
  2256. static const struct ocp_sma_op ocp_adva_sma_op = {
  2257. .tbl = { ptp_ocp_adva_sma_in, ptp_ocp_adva_sma_out },
  2258. .init = ptp_ocp_sma_fb_init,
  2259. .get = ptp_ocp_sma_fb_get,
  2260. .set_inputs = ptp_ocp_sma_adva_set_inputs,
  2261. .set_output = ptp_ocp_sma_adva_set_output,
  2262. };
  2263. static int
  2264. ptp_ocp_set_pins(struct ptp_ocp *bp)
  2265. {
  2266. struct ptp_pin_desc *config;
  2267. int i;
  2268. config = kzalloc_objs(*config, 4);
  2269. if (!config)
  2270. return -ENOMEM;
  2271. for (i = 0; i < 4; i++) {
  2272. sprintf(config[i].name, "sma%d", i + 1);
  2273. config[i].index = i;
  2274. }
  2275. bp->ptp_info.n_pins = 4;
  2276. bp->ptp_info.pin_config = config;
  2277. return 0;
  2278. }
  2279. static void
  2280. ptp_ocp_fb_set_version(struct ptp_ocp *bp)
  2281. {
  2282. u64 cap = OCP_CAP_BASIC;
  2283. u32 version;
  2284. version = ioread32(&bp->image->version);
  2285. /* if lower 16 bits are empty, this is the fw loader. */
  2286. if ((version & 0xffff) == 0) {
  2287. version = version >> 16;
  2288. bp->fw_loader = true;
  2289. }
  2290. bp->fw_tag = version >> 15;
  2291. bp->fw_version = version & 0x7fff;
  2292. if (bp->fw_tag) {
  2293. /* FPGA firmware */
  2294. if (version >= 5)
  2295. cap |= OCP_CAP_SIGNAL | OCP_CAP_FREQ;
  2296. } else {
  2297. /* SOM firmware */
  2298. if (version >= 19)
  2299. cap |= OCP_CAP_SIGNAL;
  2300. if (version >= 20)
  2301. cap |= OCP_CAP_FREQ;
  2302. }
  2303. bp->fw_cap = cap;
  2304. }
  2305. /* FB specific board initializers; last "resource" registered. */
  2306. static int
  2307. ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
  2308. {
  2309. int err;
  2310. bp->flash_start = 1024 * 4096;
  2311. bp->eeprom_map = fb_eeprom_map;
  2312. bp->fw_version = ioread32(&bp->image->version);
  2313. bp->sma_op = &ocp_fb_sma_op;
  2314. bp->signals_nr = 4;
  2315. bp->freq_in_nr = 4;
  2316. ptp_ocp_fb_set_version(bp);
  2317. ptp_ocp_tod_init(bp);
  2318. ptp_ocp_nmea_out_init(bp);
  2319. ptp_ocp_signal_init(bp);
  2320. err = ptp_ocp_attr_group_add(bp, fb_timecard_groups);
  2321. if (err)
  2322. return err;
  2323. err = ptp_ocp_set_pins(bp);
  2324. if (err)
  2325. return err;
  2326. ptp_ocp_sma_init(bp);
  2327. return ptp_ocp_init_clock(bp, r->extra);
  2328. }
  2329. static bool
  2330. ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r)
  2331. {
  2332. bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs;
  2333. if (!allow)
  2334. dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n",
  2335. r->irq_vec, r->name);
  2336. return allow;
  2337. }
  2338. static int
  2339. ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
  2340. {
  2341. struct ocp_resource *r, *table;
  2342. int err = 0;
  2343. table = (struct ocp_resource *)driver_data;
  2344. for (r = table; r->setup; r++) {
  2345. if (!ptp_ocp_allow_irq(bp, r))
  2346. continue;
  2347. err = r->setup(bp, r);
  2348. if (err) {
  2349. dev_err(&bp->pdev->dev,
  2350. "Could not register %s: err %d\n",
  2351. r->name, err);
  2352. break;
  2353. }
  2354. }
  2355. return err;
  2356. }
  2357. static void
  2358. ptp_ocp_art_sma_init(struct ptp_ocp *bp)
  2359. {
  2360. struct dpll_pin_properties prop = {
  2361. .board_label = NULL,
  2362. .type = DPLL_PIN_TYPE_EXT,
  2363. .capabilities = 0,
  2364. .freq_supported_num = ARRAY_SIZE(ptp_ocp_sma_freq),
  2365. .freq_supported = ptp_ocp_sma_freq,
  2366. };
  2367. u32 reg;
  2368. int i;
  2369. /* defaults */
  2370. bp->sma[0].mode = SMA_MODE_IN;
  2371. bp->sma[1].mode = SMA_MODE_IN;
  2372. bp->sma[2].mode = SMA_MODE_OUT;
  2373. bp->sma[3].mode = SMA_MODE_OUT;
  2374. bp->sma[0].default_fcn = 0x08; /* IN: 10Mhz */
  2375. bp->sma[1].default_fcn = 0x01; /* IN: PPS1 */
  2376. bp->sma[2].default_fcn = 0x10; /* OUT: 10Mhz */
  2377. bp->sma[3].default_fcn = 0x02; /* OUT: PHC */
  2378. for (i = 0; i < OCP_SMA_NUM; i++) {
  2379. /* If no SMA map, the pin functions and directions are fixed. */
  2380. bp->sma[i].dpll_prop = prop;
  2381. bp->sma[i].dpll_prop.board_label =
  2382. bp->ptp_info.pin_config[i].name;
  2383. if (!bp->art_sma) {
  2384. bp->sma[i].fixed_fcn = true;
  2385. bp->sma[i].fixed_dir = true;
  2386. continue;
  2387. }
  2388. reg = ioread32(&bp->art_sma->map[i].gpio);
  2389. switch (reg & 0xff) {
  2390. case 0:
  2391. bp->sma[i].fixed_fcn = true;
  2392. bp->sma[i].fixed_dir = true;
  2393. break;
  2394. case 1:
  2395. case 8:
  2396. bp->sma[i].mode = SMA_MODE_IN;
  2397. bp->sma[i].dpll_prop.capabilities =
  2398. DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE;
  2399. break;
  2400. default:
  2401. bp->sma[i].mode = SMA_MODE_OUT;
  2402. bp->sma[i].dpll_prop.capabilities =
  2403. DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE;
  2404. break;
  2405. }
  2406. }
  2407. }
  2408. static u32
  2409. ptp_ocp_art_sma_get(struct ptp_ocp *bp, int sma_nr)
  2410. {
  2411. if (bp->sma[sma_nr - 1].fixed_fcn)
  2412. return bp->sma[sma_nr - 1].default_fcn;
  2413. return ioread32(&bp->art_sma->map[sma_nr - 1].gpio) & 0xff;
  2414. }
  2415. /* note: store 0 is considered invalid. */
  2416. static int
  2417. ptp_ocp_art_sma_set(struct ptp_ocp *bp, int sma_nr, u32 val)
  2418. {
  2419. unsigned long flags;
  2420. u32 __iomem *gpio;
  2421. int err = 0;
  2422. u32 reg;
  2423. val &= SMA_SELECT_MASK;
  2424. if (hweight32(val) > 1)
  2425. return -EINVAL;
  2426. gpio = &bp->art_sma->map[sma_nr - 1].gpio;
  2427. spin_lock_irqsave(&bp->lock, flags);
  2428. reg = ioread32(gpio);
  2429. if (((reg >> 16) & val) == 0) {
  2430. err = -EOPNOTSUPP;
  2431. } else {
  2432. reg = (reg & 0xff00) | (val & 0xff);
  2433. iowrite32(reg, gpio);
  2434. }
  2435. spin_unlock_irqrestore(&bp->lock, flags);
  2436. return err;
  2437. }
  2438. static const struct ocp_sma_op ocp_art_sma_op = {
  2439. .tbl = { ptp_ocp_art_sma_in, ptp_ocp_art_sma_out },
  2440. .init = ptp_ocp_art_sma_init,
  2441. .get = ptp_ocp_art_sma_get,
  2442. .set_inputs = ptp_ocp_art_sma_set,
  2443. .set_output = ptp_ocp_art_sma_set,
  2444. };
  2445. /* ART specific board initializers; last "resource" registered. */
  2446. static int
  2447. ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
  2448. {
  2449. int err;
  2450. bp->flash_start = 0x1000000;
  2451. bp->eeprom_map = art_eeprom_map;
  2452. bp->fw_cap = OCP_CAP_BASIC;
  2453. bp->fw_version = ioread32(&bp->reg->version);
  2454. bp->fw_tag = 2;
  2455. bp->sma_op = &ocp_art_sma_op;
  2456. bp->signals_nr = 4;
  2457. bp->freq_in_nr = 4;
  2458. /* Enable MAC serial port during initialisation */
  2459. iowrite32(1, &bp->board_config->mro50_serial_activate);
  2460. err = ptp_ocp_set_pins(bp);
  2461. if (err)
  2462. return err;
  2463. ptp_ocp_sma_init(bp);
  2464. err = ptp_ocp_attr_group_add(bp, art_timecard_groups);
  2465. if (err)
  2466. return err;
  2467. return ptp_ocp_init_clock(bp, r->extra);
  2468. }
  2469. /* ADVA specific board initializers; last "resource" registered. */
  2470. static int
  2471. ptp_ocp_adva_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
  2472. {
  2473. int err;
  2474. u32 version;
  2475. bp->flash_start = 0xA00000;
  2476. bp->eeprom_map = fb_eeprom_map;
  2477. bp->sma_op = &ocp_adva_sma_op;
  2478. bp->signals_nr = 2;
  2479. bp->freq_in_nr = 2;
  2480. version = ioread32(&bp->image->version);
  2481. /* if lower 16 bits are empty, this is the fw loader. */
  2482. if ((version & 0xffff) == 0) {
  2483. version = version >> 16;
  2484. bp->fw_loader = true;
  2485. }
  2486. bp->fw_tag = 3;
  2487. bp->fw_version = version & 0xffff;
  2488. bp->fw_cap = OCP_CAP_BASIC | OCP_CAP_SIGNAL | OCP_CAP_FREQ;
  2489. ptp_ocp_tod_init(bp);
  2490. ptp_ocp_nmea_out_init(bp);
  2491. ptp_ocp_signal_init(bp);
  2492. err = ptp_ocp_attr_group_add(bp, adva_timecard_groups);
  2493. if (err)
  2494. return err;
  2495. err = ptp_ocp_set_pins(bp);
  2496. if (err)
  2497. return err;
  2498. ptp_ocp_sma_init(bp);
  2499. return ptp_ocp_init_clock(bp, r->extra);
  2500. }
  2501. static ssize_t
  2502. ptp_ocp_show_output(const struct ocp_selector *tbl, u32 val, char *buf,
  2503. int def_val)
  2504. {
  2505. const char *name;
  2506. ssize_t count;
  2507. count = sysfs_emit(buf, "OUT: ");
  2508. name = ptp_ocp_select_name_from_val(tbl, val);
  2509. if (!name)
  2510. name = ptp_ocp_select_name_from_val(tbl, def_val);
  2511. count += sysfs_emit_at(buf, count, "%s\n", name);
  2512. return count;
  2513. }
  2514. static ssize_t
  2515. ptp_ocp_show_inputs(const struct ocp_selector *tbl, u32 val, char *buf,
  2516. int def_val)
  2517. {
  2518. const char *name;
  2519. ssize_t count;
  2520. int i;
  2521. count = sysfs_emit(buf, "IN: ");
  2522. for (i = 0; tbl[i].name; i++) {
  2523. if (val & tbl[i].value) {
  2524. name = tbl[i].name;
  2525. count += sysfs_emit_at(buf, count, "%s ", name);
  2526. }
  2527. }
  2528. if (!val && def_val >= 0) {
  2529. name = ptp_ocp_select_name_from_val(tbl, def_val);
  2530. count += sysfs_emit_at(buf, count, "%s ", name);
  2531. }
  2532. if (count)
  2533. count--;
  2534. count += sysfs_emit_at(buf, count, "\n");
  2535. return count;
  2536. }
  2537. static int
  2538. sma_parse_inputs(const struct ocp_selector * const tbl[], const char *buf,
  2539. enum ptp_ocp_sma_mode *mode)
  2540. {
  2541. int idx, count, dir;
  2542. char **argv;
  2543. int ret;
  2544. argv = argv_split(GFP_KERNEL, buf, &count);
  2545. if (!argv)
  2546. return -ENOMEM;
  2547. ret = -EINVAL;
  2548. if (!count)
  2549. goto out;
  2550. idx = 0;
  2551. dir = *mode == SMA_MODE_IN ? 0 : 1;
  2552. if (!strcasecmp("IN:", argv[0])) {
  2553. dir = 0;
  2554. idx++;
  2555. }
  2556. if (!strcasecmp("OUT:", argv[0])) {
  2557. dir = 1;
  2558. idx++;
  2559. }
  2560. *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT;
  2561. ret = 0;
  2562. for (; idx < count; idx++)
  2563. ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);
  2564. if (ret < 0)
  2565. ret = -EINVAL;
  2566. out:
  2567. argv_free(argv);
  2568. return ret;
  2569. }
  2570. static ssize_t
  2571. ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf,
  2572. int default_in_val, int default_out_val)
  2573. {
  2574. struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
  2575. const struct ocp_selector * const *tbl;
  2576. u32 val;
  2577. tbl = bp->sma_op->tbl;
  2578. val = ptp_ocp_sma_get(bp, sma_nr) & SMA_SELECT_MASK;
  2579. if (sma->mode == SMA_MODE_IN) {
  2580. if (sma->disabled)
  2581. val = SMA_DISABLE;
  2582. return ptp_ocp_show_inputs(tbl[0], val, buf, default_in_val);
  2583. }
  2584. return ptp_ocp_show_output(tbl[1], val, buf, default_out_val);
  2585. }
  2586. static ssize_t
  2587. sma1_show(struct device *dev, struct device_attribute *attr, char *buf)
  2588. {
  2589. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2590. return ptp_ocp_sma_show(bp, 1, buf, 0, 1);
  2591. }
  2592. static ssize_t
  2593. sma2_show(struct device *dev, struct device_attribute *attr, char *buf)
  2594. {
  2595. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2596. return ptp_ocp_sma_show(bp, 2, buf, -1, 1);
  2597. }
  2598. static ssize_t
  2599. sma3_show(struct device *dev, struct device_attribute *attr, char *buf)
  2600. {
  2601. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2602. return ptp_ocp_sma_show(bp, 3, buf, -1, 0);
  2603. }
  2604. static ssize_t
  2605. sma4_show(struct device *dev, struct device_attribute *attr, char *buf)
  2606. {
  2607. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2608. return ptp_ocp_sma_show(bp, 4, buf, -1, 1);
  2609. }
  2610. static int
  2611. ptp_ocp_sma_store_val(struct ptp_ocp *bp, int val, enum ptp_ocp_sma_mode mode, int sma_nr)
  2612. {
  2613. struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
  2614. if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE))
  2615. return -EOPNOTSUPP;
  2616. if (sma->fixed_fcn) {
  2617. if (val != sma->default_fcn)
  2618. return -EOPNOTSUPP;
  2619. return 0;
  2620. }
  2621. sma->disabled = !!(val & SMA_DISABLE);
  2622. if (mode != sma->mode) {
  2623. if (mode == SMA_MODE_IN)
  2624. ptp_ocp_sma_set_output(bp, sma_nr, 0);
  2625. else
  2626. ptp_ocp_sma_set_inputs(bp, sma_nr, 0);
  2627. sma->mode = mode;
  2628. }
  2629. if (!sma->fixed_dir)
  2630. val |= SMA_ENABLE; /* add enable bit */
  2631. if (sma->disabled)
  2632. val = 0;
  2633. if (mode == SMA_MODE_IN)
  2634. val = ptp_ocp_sma_set_inputs(bp, sma_nr, val);
  2635. else
  2636. val = ptp_ocp_sma_set_output(bp, sma_nr, val);
  2637. return val;
  2638. }
  2639. static int
  2640. ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr)
  2641. {
  2642. struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
  2643. enum ptp_ocp_sma_mode mode;
  2644. int val;
  2645. mode = sma->mode;
  2646. val = sma_parse_inputs(bp->sma_op->tbl, buf, &mode);
  2647. if (val < 0)
  2648. return val;
  2649. return ptp_ocp_sma_store_val(bp, val, mode, sma_nr);
  2650. }
  2651. static ssize_t
  2652. sma1_store(struct device *dev, struct device_attribute *attr,
  2653. const char *buf, size_t count)
  2654. {
  2655. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2656. int err;
  2657. err = ptp_ocp_sma_store(bp, buf, 1);
  2658. return err ? err : count;
  2659. }
  2660. static ssize_t
  2661. sma2_store(struct device *dev, struct device_attribute *attr,
  2662. const char *buf, size_t count)
  2663. {
  2664. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2665. int err;
  2666. err = ptp_ocp_sma_store(bp, buf, 2);
  2667. return err ? err : count;
  2668. }
  2669. static ssize_t
  2670. sma3_store(struct device *dev, struct device_attribute *attr,
  2671. const char *buf, size_t count)
  2672. {
  2673. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2674. int err;
  2675. err = ptp_ocp_sma_store(bp, buf, 3);
  2676. return err ? err : count;
  2677. }
  2678. static ssize_t
  2679. sma4_store(struct device *dev, struct device_attribute *attr,
  2680. const char *buf, size_t count)
  2681. {
  2682. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2683. int err;
  2684. err = ptp_ocp_sma_store(bp, buf, 4);
  2685. return err ? err : count;
  2686. }
  2687. static DEVICE_ATTR_RW(sma1);
  2688. static DEVICE_ATTR_RW(sma2);
  2689. static DEVICE_ATTR_RW(sma3);
  2690. static DEVICE_ATTR_RW(sma4);
  2691. static ssize_t
  2692. available_sma_inputs_show(struct device *dev,
  2693. struct device_attribute *attr, char *buf)
  2694. {
  2695. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2696. return ptp_ocp_select_table_show(bp->sma_op->tbl[0], buf);
  2697. }
  2698. static DEVICE_ATTR_RO(available_sma_inputs);
  2699. static ssize_t
  2700. available_sma_outputs_show(struct device *dev,
  2701. struct device_attribute *attr, char *buf)
  2702. {
  2703. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2704. return ptp_ocp_select_table_show(bp->sma_op->tbl[1], buf);
  2705. }
  2706. static DEVICE_ATTR_RO(available_sma_outputs);
  2707. #define EXT_ATTR_RO(_group, _name, _val) \
  2708. struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \
  2709. { __ATTR_RO(_name), (void *)_val }
  2710. #define EXT_ATTR_RW(_group, _name, _val) \
  2711. struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \
  2712. { __ATTR_RW(_name), (void *)_val }
  2713. #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
  2714. /* period [duty [phase [polarity]]] */
  2715. static ssize_t
  2716. signal_store(struct device *dev, struct device_attribute *attr,
  2717. const char *buf, size_t count)
  2718. {
  2719. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2720. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2721. struct ptp_ocp_signal s = { };
  2722. int gen = (uintptr_t)ea->var;
  2723. int argc, err;
  2724. char **argv;
  2725. argv = argv_split(GFP_KERNEL, buf, &argc);
  2726. if (!argv)
  2727. return -ENOMEM;
  2728. err = -EINVAL;
  2729. s.duty = bp->signal[gen].duty;
  2730. s.phase = bp->signal[gen].phase;
  2731. s.period = bp->signal[gen].period;
  2732. s.polarity = bp->signal[gen].polarity;
  2733. switch (argc) {
  2734. case 4:
  2735. argc--;
  2736. err = kstrtobool(argv[argc], &s.polarity);
  2737. if (err)
  2738. goto out;
  2739. fallthrough;
  2740. case 3:
  2741. argc--;
  2742. err = kstrtou64(argv[argc], 0, &s.phase);
  2743. if (err)
  2744. goto out;
  2745. fallthrough;
  2746. case 2:
  2747. argc--;
  2748. err = kstrtoint(argv[argc], 0, &s.duty);
  2749. if (err)
  2750. goto out;
  2751. fallthrough;
  2752. case 1:
  2753. argc--;
  2754. err = kstrtou64(argv[argc], 0, &s.period);
  2755. if (err)
  2756. goto out;
  2757. break;
  2758. default:
  2759. goto out;
  2760. }
  2761. err = ptp_ocp_signal_set(bp, gen, &s);
  2762. if (err)
  2763. goto out;
  2764. err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0);
  2765. out:
  2766. argv_free(argv);
  2767. return err ? err : count;
  2768. }
  2769. static ssize_t
  2770. signal_show(struct device *dev, struct device_attribute *attr, char *buf)
  2771. {
  2772. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2773. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2774. struct ptp_ocp_signal *signal;
  2775. int gen = (uintptr_t)ea->var;
  2776. struct timespec64 ts;
  2777. signal = &bp->signal[gen];
  2778. ts = ktime_to_timespec64(signal->start);
  2779. return sysfs_emit(buf, "%llu %d %llu %d %ptT TAI\n",
  2780. signal->period, signal->duty, signal->phase, signal->polarity,
  2781. &ts.tv_sec);
  2782. }
  2783. static EXT_ATTR_RW(signal, signal, 0);
  2784. static EXT_ATTR_RW(signal, signal, 1);
  2785. static EXT_ATTR_RW(signal, signal, 2);
  2786. static EXT_ATTR_RW(signal, signal, 3);
  2787. static ssize_t
  2788. duty_show(struct device *dev, struct device_attribute *attr, char *buf)
  2789. {
  2790. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2791. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2792. int i = (uintptr_t)ea->var;
  2793. return sysfs_emit(buf, "%d\n", bp->signal[i].duty);
  2794. }
  2795. static EXT_ATTR_RO(signal, duty, 0);
  2796. static EXT_ATTR_RO(signal, duty, 1);
  2797. static EXT_ATTR_RO(signal, duty, 2);
  2798. static EXT_ATTR_RO(signal, duty, 3);
  2799. static ssize_t
  2800. period_show(struct device *dev, struct device_attribute *attr, char *buf)
  2801. {
  2802. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2803. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2804. int i = (uintptr_t)ea->var;
  2805. return sysfs_emit(buf, "%llu\n", bp->signal[i].period);
  2806. }
  2807. static EXT_ATTR_RO(signal, period, 0);
  2808. static EXT_ATTR_RO(signal, period, 1);
  2809. static EXT_ATTR_RO(signal, period, 2);
  2810. static EXT_ATTR_RO(signal, period, 3);
  2811. static ssize_t
  2812. phase_show(struct device *dev, struct device_attribute *attr, char *buf)
  2813. {
  2814. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2815. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2816. int i = (uintptr_t)ea->var;
  2817. return sysfs_emit(buf, "%llu\n", bp->signal[i].phase);
  2818. }
  2819. static EXT_ATTR_RO(signal, phase, 0);
  2820. static EXT_ATTR_RO(signal, phase, 1);
  2821. static EXT_ATTR_RO(signal, phase, 2);
  2822. static EXT_ATTR_RO(signal, phase, 3);
  2823. static ssize_t
  2824. polarity_show(struct device *dev, struct device_attribute *attr,
  2825. char *buf)
  2826. {
  2827. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2828. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2829. int i = (uintptr_t)ea->var;
  2830. return sysfs_emit(buf, "%d\n", bp->signal[i].polarity);
  2831. }
  2832. static EXT_ATTR_RO(signal, polarity, 0);
  2833. static EXT_ATTR_RO(signal, polarity, 1);
  2834. static EXT_ATTR_RO(signal, polarity, 2);
  2835. static EXT_ATTR_RO(signal, polarity, 3);
  2836. static ssize_t
  2837. running_show(struct device *dev, struct device_attribute *attr, char *buf)
  2838. {
  2839. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2840. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2841. int i = (uintptr_t)ea->var;
  2842. return sysfs_emit(buf, "%d\n", bp->signal[i].running);
  2843. }
  2844. static EXT_ATTR_RO(signal, running, 0);
  2845. static EXT_ATTR_RO(signal, running, 1);
  2846. static EXT_ATTR_RO(signal, running, 2);
  2847. static EXT_ATTR_RO(signal, running, 3);
  2848. static ssize_t
  2849. start_show(struct device *dev, struct device_attribute *attr, char *buf)
  2850. {
  2851. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2852. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2853. int i = (uintptr_t)ea->var;
  2854. struct timespec64 ts;
  2855. ts = ktime_to_timespec64(bp->signal[i].start);
  2856. return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec);
  2857. }
  2858. static EXT_ATTR_RO(signal, start, 0);
  2859. static EXT_ATTR_RO(signal, start, 1);
  2860. static EXT_ATTR_RO(signal, start, 2);
  2861. static EXT_ATTR_RO(signal, start, 3);
  2862. static ssize_t
  2863. seconds_store(struct device *dev, struct device_attribute *attr,
  2864. const char *buf, size_t count)
  2865. {
  2866. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2867. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2868. int idx = (uintptr_t)ea->var;
  2869. u32 val;
  2870. int err;
  2871. err = kstrtou32(buf, 0, &val);
  2872. if (err)
  2873. return err;
  2874. if (val > 0xff)
  2875. return -EINVAL;
  2876. if (val)
  2877. val = (val << 8) | 0x1;
  2878. iowrite32(val, &bp->freq_in[idx]->ctrl);
  2879. return count;
  2880. }
  2881. static ssize_t
  2882. seconds_show(struct device *dev, struct device_attribute *attr, char *buf)
  2883. {
  2884. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2885. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2886. int idx = (uintptr_t)ea->var;
  2887. u32 val;
  2888. val = ioread32(&bp->freq_in[idx]->ctrl);
  2889. if (val & 1)
  2890. val = (val >> 8) & 0xff;
  2891. else
  2892. val = 0;
  2893. return sysfs_emit(buf, "%u\n", val);
  2894. }
  2895. static EXT_ATTR_RW(freq, seconds, 0);
  2896. static EXT_ATTR_RW(freq, seconds, 1);
  2897. static EXT_ATTR_RW(freq, seconds, 2);
  2898. static EXT_ATTR_RW(freq, seconds, 3);
  2899. static ssize_t
  2900. frequency_show(struct device *dev, struct device_attribute *attr, char *buf)
  2901. {
  2902. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2903. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2904. int idx = (uintptr_t)ea->var;
  2905. u32 val;
  2906. val = ioread32(&bp->freq_in[idx]->status);
  2907. if (val & FREQ_STATUS_ERROR)
  2908. return sysfs_emit(buf, "error\n");
  2909. if (val & FREQ_STATUS_OVERRUN)
  2910. return sysfs_emit(buf, "overrun\n");
  2911. if (val & FREQ_STATUS_VALID)
  2912. return sysfs_emit(buf, "%lu\n", val & FREQ_STATUS_MASK);
  2913. return 0;
  2914. }
  2915. static EXT_ATTR_RO(freq, frequency, 0);
  2916. static EXT_ATTR_RO(freq, frequency, 1);
  2917. static EXT_ATTR_RO(freq, frequency, 2);
  2918. static EXT_ATTR_RO(freq, frequency, 3);
  2919. static ssize_t
  2920. ptp_ocp_tty_show(struct device *dev, struct device_attribute *attr, char *buf)
  2921. {
  2922. struct dev_ext_attribute *ea = to_ext_attr(attr);
  2923. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2924. /*
  2925. * NOTE: This output does not include a trailing newline for backward
  2926. * compatibility. Existing userspace software uses this value directly
  2927. * as a device path (e.g., "/dev/ttyS4"), and adding a newline would
  2928. * break those applications. Do not add a newline to this output.
  2929. */
  2930. return sysfs_emit(buf, "ttyS%d", bp->port[(uintptr_t)ea->var].line);
  2931. }
  2932. static umode_t
  2933. ptp_ocp_timecard_tty_is_visible(struct kobject *kobj, struct attribute *attr, int n)
  2934. {
  2935. struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
  2936. struct ptp_ocp_serial_port *port;
  2937. struct device_attribute *dattr;
  2938. struct dev_ext_attribute *ea;
  2939. if (strncmp(attr->name, "tty", 3))
  2940. return attr->mode;
  2941. dattr = container_of(attr, struct device_attribute, attr);
  2942. ea = container_of(dattr, struct dev_ext_attribute, attr);
  2943. port = &bp->port[(uintptr_t)ea->var];
  2944. return port->line == -1 ? 0 : 0444;
  2945. }
  2946. #define EXT_TTY_ATTR_RO(_name, _val) \
  2947. struct dev_ext_attribute dev_attr_tty##_name = \
  2948. { __ATTR(tty##_name, 0444, ptp_ocp_tty_show, NULL), (void *)_val }
  2949. static EXT_TTY_ATTR_RO(GNSS, PORT_GNSS);
  2950. static EXT_TTY_ATTR_RO(GNSS2, PORT_GNSS2);
  2951. static EXT_TTY_ATTR_RO(MAC, PORT_MAC);
  2952. static EXT_TTY_ATTR_RO(NMEA, PORT_NMEA);
  2953. static struct attribute *ptp_ocp_timecard_tty_attrs[] = {
  2954. &dev_attr_ttyGNSS.attr.attr,
  2955. &dev_attr_ttyGNSS2.attr.attr,
  2956. &dev_attr_ttyMAC.attr.attr,
  2957. &dev_attr_ttyNMEA.attr.attr,
  2958. NULL,
  2959. };
  2960. static const struct attribute_group ptp_ocp_timecard_tty_group = {
  2961. .name = "tty",
  2962. .attrs = ptp_ocp_timecard_tty_attrs,
  2963. .is_visible = ptp_ocp_timecard_tty_is_visible,
  2964. };
  2965. static ssize_t
  2966. serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
  2967. {
  2968. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2969. if (!bp->has_eeprom_data)
  2970. ptp_ocp_read_eeprom(bp);
  2971. return sysfs_emit(buf, "%pM\n", bp->serial);
  2972. }
  2973. static DEVICE_ATTR_RO(serialnum);
  2974. static ssize_t
  2975. gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
  2976. {
  2977. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2978. ssize_t ret;
  2979. if (bp->gnss_lost)
  2980. ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
  2981. else
  2982. ret = sysfs_emit(buf, "SYNC\n");
  2983. return ret;
  2984. }
  2985. static DEVICE_ATTR_RO(gnss_sync);
  2986. static ssize_t
  2987. utc_tai_offset_show(struct device *dev,
  2988. struct device_attribute *attr, char *buf)
  2989. {
  2990. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2991. return sysfs_emit(buf, "%d\n", bp->utc_tai_offset);
  2992. }
  2993. static ssize_t
  2994. utc_tai_offset_store(struct device *dev,
  2995. struct device_attribute *attr,
  2996. const char *buf, size_t count)
  2997. {
  2998. struct ptp_ocp *bp = dev_get_drvdata(dev);
  2999. int err;
  3000. u32 val;
  3001. err = kstrtou32(buf, 0, &val);
  3002. if (err)
  3003. return err;
  3004. ptp_ocp_utc_distribute(bp, val);
  3005. return count;
  3006. }
  3007. static DEVICE_ATTR_RW(utc_tai_offset);
  3008. static ssize_t
  3009. ts_window_adjust_show(struct device *dev,
  3010. struct device_attribute *attr, char *buf)
  3011. {
  3012. struct ptp_ocp *bp = dev_get_drvdata(dev);
  3013. return sysfs_emit(buf, "%d\n", bp->ts_window_adjust);
  3014. }
  3015. static ssize_t
  3016. ts_window_adjust_store(struct device *dev,
  3017. struct device_attribute *attr,
  3018. const char *buf, size_t count)
  3019. {
  3020. struct ptp_ocp *bp = dev_get_drvdata(dev);
  3021. int err;
  3022. u32 val;
  3023. err = kstrtou32(buf, 0, &val);
  3024. if (err)
  3025. return err;
  3026. bp->ts_window_adjust = val;
  3027. return count;
  3028. }
  3029. static DEVICE_ATTR_RW(ts_window_adjust);
  3030. static ssize_t
  3031. irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  3032. {
  3033. struct ptp_ocp *bp = dev_get_drvdata(dev);
  3034. u32 val;
  3035. val = ioread32(&bp->irig_out->ctrl);
  3036. val = (val >> 16) & 0x07;
  3037. return sysfs_emit(buf, "%d\n", val);
  3038. }
  3039. static ssize_t
  3040. irig_b_mode_store(struct device *dev,
  3041. struct device_attribute *attr,
  3042. const char *buf, size_t count)
  3043. {
  3044. struct ptp_ocp *bp = dev_get_drvdata(dev);
  3045. unsigned long flags;
  3046. int err;
  3047. u32 reg;
  3048. u8 val;
  3049. err = kstrtou8(buf, 0, &val);
  3050. if (err)
  3051. return err;
  3052. if (val > 7)
  3053. return -EINVAL;
  3054. reg = ((val & 0x7) << 16);
  3055. spin_lock_irqsave(&bp->lock, flags);
  3056. iowrite32(0, &bp->irig_out->ctrl); /* disable */
  3057. iowrite32(reg, &bp->irig_out->ctrl); /* change mode */
  3058. iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl);
  3059. spin_unlock_irqrestore(&bp->lock, flags);
  3060. return count;
  3061. }
  3062. static DEVICE_ATTR_RW(irig_b_mode);
  3063. static ssize_t
  3064. clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
  3065. {
  3066. struct ptp_ocp *bp = dev_get_drvdata(dev);
  3067. const char *p;
  3068. u32 select;
  3069. select = ioread32(&bp->reg->select);
  3070. p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16);
  3071. return sysfs_emit(buf, "%s\n", p);
  3072. }
  3073. static ssize_t
  3074. clock_source_store(struct device *dev, struct device_attribute *attr,
  3075. const char *buf, size_t count)
  3076. {
  3077. struct ptp_ocp *bp = dev_get_drvdata(dev);
  3078. unsigned long flags;
  3079. int val;
  3080. val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf);
  3081. if (val < 0)
  3082. return val;
  3083. spin_lock_irqsave(&bp->lock, flags);
  3084. iowrite32(val, &bp->reg->select);
  3085. spin_unlock_irqrestore(&bp->lock, flags);
  3086. return count;
  3087. }
  3088. static DEVICE_ATTR_RW(clock_source);
  3089. static ssize_t
  3090. available_clock_sources_show(struct device *dev,
  3091. struct device_attribute *attr, char *buf)
  3092. {
  3093. return ptp_ocp_select_table_show(ptp_ocp_clock, buf);
  3094. }
  3095. static DEVICE_ATTR_RO(available_clock_sources);
  3096. static ssize_t
  3097. clock_status_drift_show(struct device *dev,
  3098. struct device_attribute *attr, char *buf)
  3099. {
  3100. struct ptp_ocp *bp = dev_get_drvdata(dev);
  3101. u32 val;
  3102. int res;
  3103. val = ioread32(&bp->reg->status_drift);
  3104. res = (val & ~INT_MAX) ? -1 : 1;
  3105. res *= (val & INT_MAX);
  3106. return sysfs_emit(buf, "%d\n", res);
  3107. }
  3108. static DEVICE_ATTR_RO(clock_status_drift);
  3109. static ssize_t
  3110. clock_status_offset_show(struct device *dev,
  3111. struct device_attribute *attr, char *buf)
  3112. {
  3113. struct ptp_ocp *bp = dev_get_drvdata(dev);
  3114. u32 val;
  3115. int res;
  3116. val = ioread32(&bp->reg->status_offset);
  3117. res = (val & ~INT_MAX) ? -1 : 1;
  3118. res *= (val & INT_MAX);
  3119. return sysfs_emit(buf, "%d\n", res);
  3120. }
  3121. static DEVICE_ATTR_RO(clock_status_offset);
  3122. static ssize_t
  3123. tod_correction_show(struct device *dev,
  3124. struct device_attribute *attr, char *buf)
  3125. {
  3126. struct ptp_ocp *bp = dev_get_drvdata(dev);
  3127. u32 val;
  3128. int res;
  3129. val = ioread32(&bp->tod->adj_sec);
  3130. res = (val & ~INT_MAX) ? -1 : 1;
  3131. res *= (val & INT_MAX);
  3132. return sysfs_emit(buf, "%d\n", res);
  3133. }
  3134. static ssize_t
  3135. tod_correction_store(struct device *dev, struct device_attribute *attr,
  3136. const char *buf, size_t count)
  3137. {
  3138. struct ptp_ocp *bp = dev_get_drvdata(dev);
  3139. unsigned long flags;
  3140. int err, res;
  3141. u32 val = 0;
  3142. err = kstrtos32(buf, 0, &res);
  3143. if (err)
  3144. return err;
  3145. if (res < 0) {
  3146. res *= -1;
  3147. val |= BIT(31);
  3148. }
  3149. val |= res;
  3150. spin_lock_irqsave(&bp->lock, flags);
  3151. iowrite32(val, &bp->tod->adj_sec);
  3152. spin_unlock_irqrestore(&bp->lock, flags);
  3153. return count;
  3154. }
  3155. static DEVICE_ATTR_RW(tod_correction);
  3156. #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr) \
  3157. static struct attribute *fb_timecard_signal##_nr##_attrs[] = { \
  3158. &dev_attr_signal##_nr##_signal.attr.attr, \
  3159. &dev_attr_signal##_nr##_duty.attr.attr, \
  3160. &dev_attr_signal##_nr##_phase.attr.attr, \
  3161. &dev_attr_signal##_nr##_period.attr.attr, \
  3162. &dev_attr_signal##_nr##_polarity.attr.attr, \
  3163. &dev_attr_signal##_nr##_running.attr.attr, \
  3164. &dev_attr_signal##_nr##_start.attr.attr, \
  3165. NULL, \
  3166. }
  3167. #define DEVICE_SIGNAL_GROUP(_name, _nr) \
  3168. _DEVICE_SIGNAL_GROUP_ATTRS(_nr); \
  3169. static const struct attribute_group \
  3170. fb_timecard_signal##_nr##_group = { \
  3171. .name = #_name, \
  3172. .attrs = fb_timecard_signal##_nr##_attrs, \
  3173. }
  3174. DEVICE_SIGNAL_GROUP(gen1, 0);
  3175. DEVICE_SIGNAL_GROUP(gen2, 1);
  3176. DEVICE_SIGNAL_GROUP(gen3, 2);
  3177. DEVICE_SIGNAL_GROUP(gen4, 3);
  3178. #define _DEVICE_FREQ_GROUP_ATTRS(_nr) \
  3179. static struct attribute *fb_timecard_freq##_nr##_attrs[] = { \
  3180. &dev_attr_freq##_nr##_seconds.attr.attr, \
  3181. &dev_attr_freq##_nr##_frequency.attr.attr, \
  3182. NULL, \
  3183. }
  3184. #define DEVICE_FREQ_GROUP(_name, _nr) \
  3185. _DEVICE_FREQ_GROUP_ATTRS(_nr); \
  3186. static const struct attribute_group \
  3187. fb_timecard_freq##_nr##_group = { \
  3188. .name = #_name, \
  3189. .attrs = fb_timecard_freq##_nr##_attrs, \
  3190. }
  3191. DEVICE_FREQ_GROUP(freq1, 0);
  3192. DEVICE_FREQ_GROUP(freq2, 1);
  3193. DEVICE_FREQ_GROUP(freq3, 2);
  3194. DEVICE_FREQ_GROUP(freq4, 3);
  3195. static ssize_t
  3196. disciplining_config_read(struct file *filp, struct kobject *kobj,
  3197. const struct bin_attribute *bin_attr, char *buf,
  3198. loff_t off, size_t count)
  3199. {
  3200. struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
  3201. size_t size = OCP_ART_CONFIG_SIZE;
  3202. struct nvmem_device *nvmem;
  3203. ssize_t err;
  3204. nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
  3205. if (IS_ERR(nvmem))
  3206. return PTR_ERR(nvmem);
  3207. if (off > size) {
  3208. err = 0;
  3209. goto out;
  3210. }
  3211. if (off + count > size)
  3212. count = size - off;
  3213. // the configuration is in the very beginning of the EEPROM
  3214. err = nvmem_device_read(nvmem, off, count, buf);
  3215. if (err != count) {
  3216. err = -EFAULT;
  3217. goto out;
  3218. }
  3219. out:
  3220. ptp_ocp_nvmem_device_put(&nvmem);
  3221. return err;
  3222. }
  3223. static ssize_t
  3224. disciplining_config_write(struct file *filp, struct kobject *kobj,
  3225. const struct bin_attribute *bin_attr, char *buf,
  3226. loff_t off, size_t count)
  3227. {
  3228. struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
  3229. struct nvmem_device *nvmem;
  3230. ssize_t err;
  3231. /* Allow write of the whole area only */
  3232. if (off || count != OCP_ART_CONFIG_SIZE)
  3233. return -EFAULT;
  3234. nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
  3235. if (IS_ERR(nvmem))
  3236. return PTR_ERR(nvmem);
  3237. err = nvmem_device_write(nvmem, 0x00, count, buf);
  3238. if (err != count)
  3239. err = -EFAULT;
  3240. ptp_ocp_nvmem_device_put(&nvmem);
  3241. return err;
  3242. }
  3243. static const BIN_ATTR_RW(disciplining_config, OCP_ART_CONFIG_SIZE);
  3244. static ssize_t
  3245. temperature_table_read(struct file *filp, struct kobject *kobj,
  3246. const struct bin_attribute *bin_attr, char *buf,
  3247. loff_t off, size_t count)
  3248. {
  3249. struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
  3250. size_t size = OCP_ART_TEMP_TABLE_SIZE;
  3251. struct nvmem_device *nvmem;
  3252. ssize_t err;
  3253. nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
  3254. if (IS_ERR(nvmem))
  3255. return PTR_ERR(nvmem);
  3256. if (off > size) {
  3257. err = 0;
  3258. goto out;
  3259. }
  3260. if (off + count > size)
  3261. count = size - off;
  3262. // the configuration is in the very beginning of the EEPROM
  3263. err = nvmem_device_read(nvmem, 0x90 + off, count, buf);
  3264. if (err != count) {
  3265. err = -EFAULT;
  3266. goto out;
  3267. }
  3268. out:
  3269. ptp_ocp_nvmem_device_put(&nvmem);
  3270. return err;
  3271. }
  3272. static ssize_t
  3273. temperature_table_write(struct file *filp, struct kobject *kobj,
  3274. const struct bin_attribute *bin_attr, char *buf,
  3275. loff_t off, size_t count)
  3276. {
  3277. struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
  3278. struct nvmem_device *nvmem;
  3279. ssize_t err;
  3280. /* Allow write of the whole area only */
  3281. if (off || count != OCP_ART_TEMP_TABLE_SIZE)
  3282. return -EFAULT;
  3283. nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
  3284. if (IS_ERR(nvmem))
  3285. return PTR_ERR(nvmem);
  3286. err = nvmem_device_write(nvmem, 0x90, count, buf);
  3287. if (err != count)
  3288. err = -EFAULT;
  3289. ptp_ocp_nvmem_device_put(&nvmem);
  3290. return err;
  3291. }
  3292. static const BIN_ATTR_RW(temperature_table, OCP_ART_TEMP_TABLE_SIZE);
  3293. static struct attribute *fb_timecard_attrs[] = {
  3294. &dev_attr_serialnum.attr,
  3295. &dev_attr_gnss_sync.attr,
  3296. &dev_attr_clock_source.attr,
  3297. &dev_attr_available_clock_sources.attr,
  3298. &dev_attr_sma1.attr,
  3299. &dev_attr_sma2.attr,
  3300. &dev_attr_sma3.attr,
  3301. &dev_attr_sma4.attr,
  3302. &dev_attr_available_sma_inputs.attr,
  3303. &dev_attr_available_sma_outputs.attr,
  3304. &dev_attr_clock_status_drift.attr,
  3305. &dev_attr_clock_status_offset.attr,
  3306. &dev_attr_irig_b_mode.attr,
  3307. &dev_attr_utc_tai_offset.attr,
  3308. &dev_attr_ts_window_adjust.attr,
  3309. &dev_attr_tod_correction.attr,
  3310. NULL,
  3311. };
  3312. static const struct attribute_group fb_timecard_group = {
  3313. .attrs = fb_timecard_attrs,
  3314. };
  3315. static const struct ocp_attr_group fb_timecard_groups[] = {
  3316. { .cap = OCP_CAP_BASIC, .group = &fb_timecard_group },
  3317. { .cap = OCP_CAP_BASIC, .group = &ptp_ocp_timecard_tty_group },
  3318. { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal0_group },
  3319. { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal1_group },
  3320. { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal2_group },
  3321. { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal3_group },
  3322. { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq0_group },
  3323. { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq1_group },
  3324. { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq2_group },
  3325. { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq3_group },
  3326. { },
  3327. };
  3328. static struct attribute *art_timecard_attrs[] = {
  3329. &dev_attr_serialnum.attr,
  3330. &dev_attr_clock_source.attr,
  3331. &dev_attr_available_clock_sources.attr,
  3332. &dev_attr_utc_tai_offset.attr,
  3333. &dev_attr_ts_window_adjust.attr,
  3334. &dev_attr_sma1.attr,
  3335. &dev_attr_sma2.attr,
  3336. &dev_attr_sma3.attr,
  3337. &dev_attr_sma4.attr,
  3338. &dev_attr_available_sma_inputs.attr,
  3339. &dev_attr_available_sma_outputs.attr,
  3340. NULL,
  3341. };
  3342. static const struct bin_attribute *const bin_art_timecard_attrs[] = {
  3343. &bin_attr_disciplining_config,
  3344. &bin_attr_temperature_table,
  3345. NULL,
  3346. };
  3347. static const struct attribute_group art_timecard_group = {
  3348. .attrs = art_timecard_attrs,
  3349. .bin_attrs = bin_art_timecard_attrs,
  3350. };
  3351. static const struct ocp_attr_group art_timecard_groups[] = {
  3352. { .cap = OCP_CAP_BASIC, .group = &art_timecard_group },
  3353. { .cap = OCP_CAP_BASIC, .group = &ptp_ocp_timecard_tty_group },
  3354. { },
  3355. };
  3356. static struct attribute *adva_timecard_attrs[] = {
  3357. &dev_attr_serialnum.attr,
  3358. &dev_attr_gnss_sync.attr,
  3359. &dev_attr_clock_source.attr,
  3360. &dev_attr_available_clock_sources.attr,
  3361. &dev_attr_sma1.attr,
  3362. &dev_attr_sma2.attr,
  3363. &dev_attr_sma3.attr,
  3364. &dev_attr_sma4.attr,
  3365. &dev_attr_available_sma_inputs.attr,
  3366. &dev_attr_available_sma_outputs.attr,
  3367. &dev_attr_clock_status_drift.attr,
  3368. &dev_attr_clock_status_offset.attr,
  3369. &dev_attr_ts_window_adjust.attr,
  3370. &dev_attr_tod_correction.attr,
  3371. NULL,
  3372. };
  3373. static const struct attribute_group adva_timecard_group = {
  3374. .attrs = adva_timecard_attrs,
  3375. };
  3376. static const struct ocp_attr_group adva_timecard_groups[] = {
  3377. { .cap = OCP_CAP_BASIC, .group = &adva_timecard_group },
  3378. { .cap = OCP_CAP_BASIC, .group = &ptp_ocp_timecard_tty_group },
  3379. { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal0_group },
  3380. { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal1_group },
  3381. { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq0_group },
  3382. { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq1_group },
  3383. { },
  3384. };
  3385. static void
  3386. gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit,
  3387. const char *def)
  3388. {
  3389. int i;
  3390. for (i = 0; i < 4; i++) {
  3391. if (bp->sma[i].mode != SMA_MODE_IN)
  3392. continue;
  3393. if (map[i][0] & (1 << bit)) {
  3394. sprintf(buf, "sma%d", i + 1);
  3395. return;
  3396. }
  3397. }
  3398. if (!def)
  3399. def = "----";
  3400. strcpy(buf, def);
  3401. }
  3402. static void
  3403. gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit)
  3404. {
  3405. char *ans = buf;
  3406. int i;
  3407. strcpy(ans, "----");
  3408. for (i = 0; i < 4; i++) {
  3409. if (bp->sma[i].mode != SMA_MODE_OUT)
  3410. continue;
  3411. if (map[i][1] & (1 << bit))
  3412. ans += sprintf(ans, "sma%d ", i + 1);
  3413. }
  3414. }
  3415. static void
  3416. _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr)
  3417. {
  3418. struct signal_reg __iomem *reg = bp->signal_out[nr]->mem;
  3419. struct ptp_ocp_signal *signal = &bp->signal[nr];
  3420. char label[16];
  3421. bool on;
  3422. u32 val;
  3423. on = signal->running;
  3424. sprintf(label, "GEN%d", nr + 1);
  3425. seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d",
  3426. label, on ? " ON" : "OFF",
  3427. signal->period, signal->duty, signal->phase,
  3428. signal->polarity);
  3429. val = ioread32(&reg->enable);
  3430. seq_printf(s, " [%x", val);
  3431. val = ioread32(&reg->status);
  3432. seq_printf(s, " %x]", val);
  3433. seq_printf(s, " start:%llu\n", signal->start);
  3434. }
  3435. static void
  3436. _frequency_summary_show(struct seq_file *s, int nr,
  3437. struct frequency_reg __iomem *reg)
  3438. {
  3439. char label[16];
  3440. bool on;
  3441. u32 val;
  3442. if (!reg)
  3443. return;
  3444. sprintf(label, "FREQ%d", nr + 1);
  3445. val = ioread32(&reg->ctrl);
  3446. on = val & 1;
  3447. val = (val >> 8) & 0xff;
  3448. seq_printf(s, "%7s: %s, sec:%u",
  3449. label,
  3450. on ? " ON" : "OFF",
  3451. val);
  3452. val = ioread32(&reg->status);
  3453. if (val & FREQ_STATUS_ERROR)
  3454. seq_printf(s, ", error");
  3455. if (val & FREQ_STATUS_OVERRUN)
  3456. seq_printf(s, ", overrun");
  3457. if (val & FREQ_STATUS_VALID)
  3458. seq_printf(s, ", freq %lu Hz", val & FREQ_STATUS_MASK);
  3459. seq_printf(s, " reg:%x\n", val);
  3460. }
  3461. static int
  3462. ptp_ocp_summary_show(struct seq_file *s, void *data)
  3463. {
  3464. struct device *dev = s->private;
  3465. struct ptp_system_timestamp sts;
  3466. struct ts_reg __iomem *ts_reg;
  3467. char *buf, *src, *mac_src;
  3468. struct timespec64 ts;
  3469. struct ptp_ocp *bp;
  3470. u16 sma_val[4][2];
  3471. u32 ctrl, val;
  3472. bool on, map;
  3473. int i;
  3474. buf = (char *)__get_free_page(GFP_KERNEL);
  3475. if (!buf)
  3476. return -ENOMEM;
  3477. bp = dev_get_drvdata(dev);
  3478. seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp));
  3479. for (i = 0; i < __PORT_COUNT; i++) {
  3480. if (bp->port[i].line != -1)
  3481. seq_printf(s, "%7s: /dev/ttyS%d\n", ptp_ocp_tty_port_name(i),
  3482. bp->port[i].line);
  3483. }
  3484. memset(sma_val, 0xff, sizeof(sma_val));
  3485. if (bp->sma_map1) {
  3486. u32 reg;
  3487. reg = ioread32(&bp->sma_map1->gpio1);
  3488. sma_val[0][0] = reg & 0xffff;
  3489. sma_val[1][0] = reg >> 16;
  3490. reg = ioread32(&bp->sma_map1->gpio2);
  3491. sma_val[2][1] = reg & 0xffff;
  3492. sma_val[3][1] = reg >> 16;
  3493. reg = ioread32(&bp->sma_map2->gpio1);
  3494. sma_val[2][0] = reg & 0xffff;
  3495. sma_val[3][0] = reg >> 16;
  3496. reg = ioread32(&bp->sma_map2->gpio2);
  3497. sma_val[0][1] = reg & 0xffff;
  3498. sma_val[1][1] = reg >> 16;
  3499. }
  3500. sma1_show(dev, NULL, buf);
  3501. seq_printf(s, " sma1: %04x,%04x %s",
  3502. sma_val[0][0], sma_val[0][1], buf);
  3503. sma2_show(dev, NULL, buf);
  3504. seq_printf(s, " sma2: %04x,%04x %s",
  3505. sma_val[1][0], sma_val[1][1], buf);
  3506. sma3_show(dev, NULL, buf);
  3507. seq_printf(s, " sma3: %04x,%04x %s",
  3508. sma_val[2][0], sma_val[2][1], buf);
  3509. sma4_show(dev, NULL, buf);
  3510. seq_printf(s, " sma4: %04x,%04x %s",
  3511. sma_val[3][0], sma_val[3][1], buf);
  3512. if (bp->ts0) {
  3513. ts_reg = bp->ts0->mem;
  3514. on = ioread32(&ts_reg->enable);
  3515. src = "GNSS1";
  3516. seq_printf(s, "%7s: %s, src: %s\n", "TS0",
  3517. on ? " ON" : "OFF", src);
  3518. }
  3519. if (bp->ts1) {
  3520. ts_reg = bp->ts1->mem;
  3521. on = ioread32(&ts_reg->enable);
  3522. gpio_input_map(buf, bp, sma_val, 2, NULL);
  3523. seq_printf(s, "%7s: %s, src: %s\n", "TS1",
  3524. on ? " ON" : "OFF", buf);
  3525. }
  3526. if (bp->ts2) {
  3527. ts_reg = bp->ts2->mem;
  3528. on = ioread32(&ts_reg->enable);
  3529. gpio_input_map(buf, bp, sma_val, 3, NULL);
  3530. seq_printf(s, "%7s: %s, src: %s\n", "TS2",
  3531. on ? " ON" : "OFF", buf);
  3532. }
  3533. if (bp->ts3) {
  3534. ts_reg = bp->ts3->mem;
  3535. on = ioread32(&ts_reg->enable);
  3536. gpio_input_map(buf, bp, sma_val, 6, NULL);
  3537. seq_printf(s, "%7s: %s, src: %s\n", "TS3",
  3538. on ? " ON" : "OFF", buf);
  3539. }
  3540. if (bp->ts4) {
  3541. ts_reg = bp->ts4->mem;
  3542. on = ioread32(&ts_reg->enable);
  3543. gpio_input_map(buf, bp, sma_val, 7, NULL);
  3544. seq_printf(s, "%7s: %s, src: %s\n", "TS4",
  3545. on ? " ON" : "OFF", buf);
  3546. }
  3547. if (bp->pps) {
  3548. ts_reg = bp->pps->mem;
  3549. src = "PHC";
  3550. on = ioread32(&ts_reg->enable);
  3551. map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP);
  3552. seq_printf(s, "%7s: %s, src: %s\n", "TS5",
  3553. on && map ? " ON" : "OFF", src);
  3554. map = !!(bp->pps_req_map & OCP_REQ_PPS);
  3555. seq_printf(s, "%7s: %s, src: %s\n", "PPS",
  3556. on && map ? " ON" : "OFF", src);
  3557. }
  3558. if (bp->fw_cap & OCP_CAP_SIGNAL)
  3559. for (i = 0; i < bp->signals_nr; i++)
  3560. _signal_summary_show(s, bp, i);
  3561. if (bp->fw_cap & OCP_CAP_FREQ)
  3562. for (i = 0; i < bp->freq_in_nr; i++)
  3563. _frequency_summary_show(s, i, bp->freq_in[i]);
  3564. if (bp->irig_out) {
  3565. ctrl = ioread32(&bp->irig_out->ctrl);
  3566. on = ctrl & IRIG_M_CTRL_ENABLE;
  3567. val = ioread32(&bp->irig_out->status);
  3568. gpio_output_map(buf, bp, sma_val, 4);
  3569. seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG",
  3570. on ? " ON" : "OFF", val, (ctrl >> 16), buf);
  3571. }
  3572. if (bp->irig_in) {
  3573. on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE;
  3574. val = ioread32(&bp->irig_in->status);
  3575. gpio_input_map(buf, bp, sma_val, 4, NULL);
  3576. seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in",
  3577. on ? " ON" : "OFF", val, buf);
  3578. }
  3579. if (bp->dcf_out) {
  3580. on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE;
  3581. val = ioread32(&bp->dcf_out->status);
  3582. gpio_output_map(buf, bp, sma_val, 5);
  3583. seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF",
  3584. on ? " ON" : "OFF", val, buf);
  3585. }
  3586. if (bp->dcf_in) {
  3587. on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE;
  3588. val = ioread32(&bp->dcf_in->status);
  3589. gpio_input_map(buf, bp, sma_val, 5, NULL);
  3590. seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in",
  3591. on ? " ON" : "OFF", val, buf);
  3592. }
  3593. if (bp->nmea_out) {
  3594. on = ioread32(&bp->nmea_out->ctrl) & 1;
  3595. val = ioread32(&bp->nmea_out->status);
  3596. seq_printf(s, "%7s: %s, error: %d\n", "NMEA",
  3597. on ? " ON" : "OFF", val);
  3598. }
  3599. /* compute src for PPS1, used below. */
  3600. if (bp->pps_select) {
  3601. val = ioread32(&bp->pps_select->gpio1);
  3602. src = &buf[80];
  3603. mac_src = "GNSS1";
  3604. if (val & 0x01) {
  3605. gpio_input_map(src, bp, sma_val, 0, NULL);
  3606. mac_src = src;
  3607. } else if (val & 0x02) {
  3608. src = "MAC";
  3609. } else if (val & 0x04) {
  3610. src = "GNSS1";
  3611. } else {
  3612. src = "----";
  3613. mac_src = src;
  3614. }
  3615. } else {
  3616. src = "?";
  3617. mac_src = src;
  3618. }
  3619. seq_printf(s, "MAC PPS1 src: %s\n", mac_src);
  3620. gpio_input_map(buf, bp, sma_val, 1, "GNSS2");
  3621. seq_printf(s, "MAC PPS2 src: %s\n", buf);
  3622. /* assumes automatic switchover/selection */
  3623. val = ioread32(&bp->reg->select);
  3624. switch (val >> 16) {
  3625. case 0:
  3626. sprintf(buf, "----");
  3627. break;
  3628. case 2:
  3629. sprintf(buf, "IRIG");
  3630. break;
  3631. case 3:
  3632. sprintf(buf, "%s via PPS1", src);
  3633. break;
  3634. case 6:
  3635. sprintf(buf, "DCF");
  3636. break;
  3637. default:
  3638. strcpy(buf, "unknown");
  3639. break;
  3640. }
  3641. seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf,
  3642. bp->sync ? "sync" : "unsynced");
  3643. if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) {
  3644. struct timespec64 sys_ts;
  3645. s64 pre_ns, post_ns, ns;
  3646. pre_ns = timespec64_to_ns(&sts.pre_ts);
  3647. post_ns = timespec64_to_ns(&sts.post_ts);
  3648. ns = (pre_ns + post_ns) / 2;
  3649. ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC;
  3650. sys_ts = ns_to_timespec64(ns);
  3651. seq_printf(s, "%7s: %ptSp == %ptS TAI\n", "PHC", &ts, &ts);
  3652. seq_printf(s, "%7s: %ptSp == %ptS UTC offset %d\n", "SYS",
  3653. &sys_ts, &sys_ts, bp->utc_tai_offset);
  3654. seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "",
  3655. timespec64_to_ns(&ts) - ns,
  3656. post_ns - pre_ns);
  3657. }
  3658. free_page((unsigned long)buf);
  3659. return 0;
  3660. }
  3661. DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);
  3662. static int
  3663. ptp_ocp_tod_status_show(struct seq_file *s, void *data)
  3664. {
  3665. struct device *dev = s->private;
  3666. struct ptp_ocp *bp;
  3667. u32 val;
  3668. int idx;
  3669. bp = dev_get_drvdata(dev);
  3670. val = ioread32(&bp->tod->ctrl);
  3671. if (!(val & TOD_CTRL_ENABLE)) {
  3672. seq_printf(s, "TOD Slave disabled\n");
  3673. return 0;
  3674. }
  3675. seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val);
  3676. idx = val & TOD_CTRL_PROTOCOL ? 4 : 0;
  3677. idx += (val >> 16) & 3;
  3678. seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx));
  3679. idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
  3680. seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx));
  3681. val = ioread32(&bp->tod->version);
  3682. seq_printf(s, "TOD Version %d.%d.%d\n",
  3683. val >> 24, (val >> 16) & 0xff, val & 0xffff);
  3684. val = ioread32(&bp->tod->status);
  3685. seq_printf(s, "Status register: 0x%08X\n", val);
  3686. val = ioread32(&bp->tod->adj_sec);
  3687. idx = (val & ~INT_MAX) ? -1 : 1;
  3688. idx *= (val & INT_MAX);
  3689. seq_printf(s, "Correction seconds: %d\n", idx);
  3690. val = ioread32(&bp->tod->utc_status);
  3691. seq_printf(s, "UTC status register: 0x%08X\n", val);
  3692. seq_printf(s, "UTC offset: %ld valid:%d\n",
  3693. val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0);
  3694. seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n",
  3695. val & TOD_STATUS_LEAP_VALID ? 1 : 0,
  3696. val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0);
  3697. val = ioread32(&bp->tod->leap);
  3698. seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val);
  3699. return 0;
  3700. }
  3701. DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status);
  3702. static struct dentry *ptp_ocp_debugfs_root;
  3703. static void
  3704. ptp_ocp_debugfs_add_device(struct ptp_ocp *bp)
  3705. {
  3706. struct dentry *d;
  3707. d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root);
  3708. bp->debug_root = d;
  3709. debugfs_create_file("summary", 0444, bp->debug_root,
  3710. &bp->dev, &ptp_ocp_summary_fops);
  3711. if (bp->tod)
  3712. debugfs_create_file("tod_status", 0444, bp->debug_root,
  3713. &bp->dev, &ptp_ocp_tod_status_fops);
  3714. }
  3715. static void
  3716. ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp)
  3717. {
  3718. debugfs_remove_recursive(bp->debug_root);
  3719. }
  3720. static void
  3721. ptp_ocp_debugfs_init(void)
  3722. {
  3723. ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL);
  3724. }
  3725. static void
  3726. ptp_ocp_debugfs_fini(void)
  3727. {
  3728. debugfs_remove_recursive(ptp_ocp_debugfs_root);
  3729. }
  3730. static void
  3731. ptp_ocp_dev_release(struct device *dev)
  3732. {
  3733. struct ptp_ocp *bp = dev_get_drvdata(dev);
  3734. mutex_lock(&ptp_ocp_lock);
  3735. idr_remove(&ptp_ocp_idr, bp->id);
  3736. mutex_unlock(&ptp_ocp_lock);
  3737. }
  3738. static int
  3739. ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
  3740. {
  3741. int i, err;
  3742. mutex_lock(&ptp_ocp_lock);
  3743. err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
  3744. mutex_unlock(&ptp_ocp_lock);
  3745. if (err < 0) {
  3746. dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
  3747. return err;
  3748. }
  3749. bp->id = err;
  3750. bp->ptp_info = ptp_ocp_clock_info;
  3751. spin_lock_init(&bp->lock);
  3752. for (i = 0; i < __PORT_COUNT; i++)
  3753. bp->port[i].line = -1;
  3754. bp->pdev = pdev;
  3755. device_initialize(&bp->dev);
  3756. dev_set_name(&bp->dev, "ocp%d", bp->id);
  3757. bp->dev.class = &timecard_class;
  3758. bp->dev.parent = &pdev->dev;
  3759. bp->dev.release = ptp_ocp_dev_release;
  3760. dev_set_drvdata(&bp->dev, bp);
  3761. err = device_add(&bp->dev);
  3762. if (err) {
  3763. dev_err(&bp->dev, "device add failed: %d\n", err);
  3764. goto out;
  3765. }
  3766. pci_set_drvdata(pdev, bp);
  3767. return 0;
  3768. out:
  3769. put_device(&bp->dev);
  3770. return err;
  3771. }
  3772. static void
  3773. ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
  3774. {
  3775. struct device *dev = &bp->dev;
  3776. if (sysfs_create_link(&dev->kobj, &child->kobj, link))
  3777. dev_err(dev, "%s symlink failed\n", link);
  3778. }
  3779. static void
  3780. ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
  3781. {
  3782. struct device *dev, *child;
  3783. dev = &bp->pdev->dev;
  3784. child = device_find_child_by_name(dev, name);
  3785. if (!child) {
  3786. dev_err(dev, "Could not find device %s\n", name);
  3787. return;
  3788. }
  3789. ptp_ocp_symlink(bp, child, link);
  3790. put_device(child);
  3791. }
  3792. static int
  3793. ptp_ocp_complete(struct ptp_ocp *bp)
  3794. {
  3795. struct pps_device *pps;
  3796. char buf[32];
  3797. sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
  3798. ptp_ocp_link_child(bp, buf, "ptp");
  3799. pps = pps_lookup_dev(bp->ptp);
  3800. if (pps)
  3801. ptp_ocp_symlink(bp, &pps->dev, "pps");
  3802. ptp_ocp_debugfs_add_device(bp);
  3803. return 0;
  3804. }
  3805. static void
  3806. ptp_ocp_phc_info(struct ptp_ocp *bp)
  3807. {
  3808. struct timespec64 ts;
  3809. u32 version, select;
  3810. version = ioread32(&bp->reg->version);
  3811. select = ioread32(&bp->reg->select);
  3812. dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
  3813. version >> 24, (version >> 16) & 0xff, version & 0xffff,
  3814. ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16),
  3815. ptp_clock_index(bp->ptp));
  3816. if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
  3817. dev_info(&bp->pdev->dev, "Time: %ptSp, %s\n",
  3818. &ts, bp->sync ? "in-sync" : "UNSYNCED");
  3819. }
  3820. static void
  3821. ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud)
  3822. {
  3823. if (port != -1)
  3824. dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud);
  3825. }
  3826. static void
  3827. ptp_ocp_info(struct ptp_ocp *bp)
  3828. {
  3829. static int nmea_baud[] = {
  3830. 1200, 2400, 4800, 9600, 19200, 38400,
  3831. 57600, 115200, 230400, 460800, 921600,
  3832. 1000000, 2000000
  3833. };
  3834. struct device *dev = &bp->pdev->dev;
  3835. u32 reg;
  3836. int i;
  3837. ptp_ocp_phc_info(bp);
  3838. for (i = 0; i < __PORT_COUNT; i++) {
  3839. if (i == PORT_NMEA && bp->nmea_out && bp->port[PORT_NMEA].line != -1) {
  3840. bp->port[PORT_NMEA].baud = -1;
  3841. reg = ioread32(&bp->nmea_out->uart_baud);
  3842. if (reg < ARRAY_SIZE(nmea_baud))
  3843. bp->port[PORT_NMEA].baud = nmea_baud[reg];
  3844. }
  3845. ptp_ocp_serial_info(dev, ptp_ocp_tty_port_name(i), bp->port[i].line,
  3846. bp->port[i].baud);
  3847. }
  3848. }
  3849. static void
  3850. ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
  3851. {
  3852. struct device *dev = &bp->dev;
  3853. sysfs_remove_link(&dev->kobj, "ptp");
  3854. sysfs_remove_link(&dev->kobj, "pps");
  3855. }
  3856. static void
  3857. ptp_ocp_detach(struct ptp_ocp *bp)
  3858. {
  3859. int i;
  3860. ptp_ocp_debugfs_remove_device(bp);
  3861. ptp_ocp_detach_sysfs(bp);
  3862. ptp_ocp_attr_group_del(bp);
  3863. timer_delete_sync(&bp->watchdog);
  3864. ptp_ocp_unregister_ext(bp->ts0);
  3865. ptp_ocp_unregister_ext(bp->ts1);
  3866. ptp_ocp_unregister_ext(bp->ts2);
  3867. ptp_ocp_unregister_ext(bp->ts3);
  3868. ptp_ocp_unregister_ext(bp->ts4);
  3869. ptp_ocp_unregister_ext(bp->pps);
  3870. for (i = 0; i < 4; i++)
  3871. ptp_ocp_unregister_ext(bp->signal_out[i]);
  3872. for (i = 0; i < __PORT_COUNT; i++)
  3873. if (bp->port[i].line != -1)
  3874. serial8250_unregister_port(bp->port[i].line);
  3875. platform_device_unregister(bp->spi_flash);
  3876. platform_device_unregister(bp->i2c_ctrl);
  3877. if (bp->i2c_clk)
  3878. clk_hw_unregister_fixed_rate(bp->i2c_clk);
  3879. if (bp->n_irqs)
  3880. pci_free_irq_vectors(bp->pdev);
  3881. if (bp->ptp)
  3882. ptp_clock_unregister(bp->ptp);
  3883. kfree(bp->ptp_info.pin_config);
  3884. device_unregister(&bp->dev);
  3885. }
  3886. static int
  3887. ptp_ocp_dpll_lock_status_get(const struct dpll_device *dpll, void *priv,
  3888. enum dpll_lock_status *status,
  3889. enum dpll_lock_status_error *status_error,
  3890. struct netlink_ext_ack *extack)
  3891. {
  3892. struct ptp_ocp *bp = priv;
  3893. *status = bp->sync ? DPLL_LOCK_STATUS_LOCKED : DPLL_LOCK_STATUS_UNLOCKED;
  3894. return 0;
  3895. }
  3896. static int ptp_ocp_dpll_state_get(const struct dpll_pin *pin, void *pin_priv,
  3897. const struct dpll_device *dpll, void *priv,
  3898. enum dpll_pin_state *state,
  3899. struct netlink_ext_ack *extack)
  3900. {
  3901. struct ptp_ocp *bp = priv;
  3902. int idx;
  3903. if (bp->pps_select) {
  3904. idx = ioread32(&bp->pps_select->gpio1);
  3905. *state = (&bp->sma[idx] == pin_priv) ? DPLL_PIN_STATE_CONNECTED :
  3906. DPLL_PIN_STATE_SELECTABLE;
  3907. return 0;
  3908. }
  3909. NL_SET_ERR_MSG(extack, "pin selection is not supported on current HW");
  3910. return -EINVAL;
  3911. }
  3912. static int ptp_ocp_dpll_mode_get(const struct dpll_device *dpll, void *priv,
  3913. enum dpll_mode *mode, struct netlink_ext_ack *extack)
  3914. {
  3915. *mode = DPLL_MODE_AUTOMATIC;
  3916. return 0;
  3917. }
  3918. static int ptp_ocp_dpll_direction_get(const struct dpll_pin *pin,
  3919. void *pin_priv,
  3920. const struct dpll_device *dpll,
  3921. void *priv,
  3922. enum dpll_pin_direction *direction,
  3923. struct netlink_ext_ack *extack)
  3924. {
  3925. struct ptp_ocp_sma_connector *sma = pin_priv;
  3926. *direction = sma->mode == SMA_MODE_IN ?
  3927. DPLL_PIN_DIRECTION_INPUT :
  3928. DPLL_PIN_DIRECTION_OUTPUT;
  3929. return 0;
  3930. }
  3931. static int ptp_ocp_dpll_direction_set(const struct dpll_pin *pin,
  3932. void *pin_priv,
  3933. const struct dpll_device *dpll,
  3934. void *dpll_priv,
  3935. enum dpll_pin_direction direction,
  3936. struct netlink_ext_ack *extack)
  3937. {
  3938. struct ptp_ocp_sma_connector *sma = pin_priv;
  3939. struct ptp_ocp *bp = dpll_priv;
  3940. enum ptp_ocp_sma_mode mode;
  3941. int sma_nr = (sma - bp->sma);
  3942. if (sma->fixed_dir)
  3943. return -EOPNOTSUPP;
  3944. mode = direction == DPLL_PIN_DIRECTION_INPUT ?
  3945. SMA_MODE_IN : SMA_MODE_OUT;
  3946. return ptp_ocp_sma_store_val(bp, 0, mode, sma_nr + 1);
  3947. }
  3948. static int ptp_ocp_dpll_frequency_set(const struct dpll_pin *pin,
  3949. void *pin_priv,
  3950. const struct dpll_device *dpll,
  3951. void *dpll_priv, u64 frequency,
  3952. struct netlink_ext_ack *extack)
  3953. {
  3954. struct ptp_ocp_sma_connector *sma = pin_priv;
  3955. struct ptp_ocp *bp = dpll_priv;
  3956. const struct ocp_selector *tbl;
  3957. int sma_nr = (sma - bp->sma);
  3958. int i;
  3959. if (sma->fixed_fcn)
  3960. return -EOPNOTSUPP;
  3961. tbl = bp->sma_op->tbl[sma->mode];
  3962. for (i = 0; tbl[i].name; i++)
  3963. if (tbl[i].frequency == frequency)
  3964. return ptp_ocp_sma_store_val(bp, i, sma->mode, sma_nr + 1);
  3965. return -EINVAL;
  3966. }
  3967. static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin,
  3968. void *pin_priv,
  3969. const struct dpll_device *dpll,
  3970. void *dpll_priv, u64 *frequency,
  3971. struct netlink_ext_ack *extack)
  3972. {
  3973. struct ptp_ocp_sma_connector *sma = pin_priv;
  3974. struct ptp_ocp *bp = dpll_priv;
  3975. const struct ocp_selector *tbl;
  3976. int sma_nr = (sma - bp->sma);
  3977. u32 val;
  3978. int i;
  3979. val = bp->sma_op->get(bp, sma_nr + 1);
  3980. tbl = bp->sma_op->tbl[sma->mode];
  3981. for (i = 0; tbl[i].name; i++)
  3982. if (val == tbl[i].value) {
  3983. *frequency = tbl[i].frequency;
  3984. return 0;
  3985. }
  3986. return -EINVAL;
  3987. }
  3988. static const struct dpll_device_ops dpll_ops = {
  3989. .lock_status_get = ptp_ocp_dpll_lock_status_get,
  3990. .mode_get = ptp_ocp_dpll_mode_get,
  3991. };
  3992. static const struct dpll_pin_ops dpll_pins_ops = {
  3993. .frequency_get = ptp_ocp_dpll_frequency_get,
  3994. .frequency_set = ptp_ocp_dpll_frequency_set,
  3995. .direction_get = ptp_ocp_dpll_direction_get,
  3996. .direction_set = ptp_ocp_dpll_direction_set,
  3997. .state_on_dpll_get = ptp_ocp_dpll_state_get,
  3998. };
  3999. static void
  4000. ptp_ocp_sync_work(struct work_struct *work)
  4001. {
  4002. struct ptp_ocp *bp;
  4003. bool sync;
  4004. bp = container_of(work, struct ptp_ocp, sync_work.work);
  4005. sync = !!(ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC);
  4006. if (bp->sync != sync)
  4007. dpll_device_change_ntf(bp->dpll);
  4008. bp->sync = sync;
  4009. queue_delayed_work(system_power_efficient_wq, &bp->sync_work, HZ);
  4010. }
  4011. static int
  4012. ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  4013. {
  4014. struct devlink *devlink;
  4015. struct ptp_ocp *bp;
  4016. int err, i;
  4017. u64 clkid;
  4018. devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev);
  4019. if (!devlink) {
  4020. dev_err(&pdev->dev, "devlink_alloc failed\n");
  4021. return -ENOMEM;
  4022. }
  4023. err = pci_enable_device(pdev);
  4024. if (err) {
  4025. dev_err(&pdev->dev, "pci_enable_device\n");
  4026. goto out_free;
  4027. }
  4028. bp = devlink_priv(devlink);
  4029. err = ptp_ocp_device_init(bp, pdev);
  4030. if (err)
  4031. goto out_disable;
  4032. INIT_DELAYED_WORK(&bp->sync_work, ptp_ocp_sync_work);
  4033. /* compat mode.
  4034. * Older FPGA firmware only returns 2 irq's.
  4035. * allow this - if not all of the IRQ's are returned, skip the
  4036. * extra devices and just register the clock.
  4037. */
  4038. err = pci_alloc_irq_vectors(pdev, 1, 17, PCI_IRQ_MSI | PCI_IRQ_MSIX);
  4039. if (err < 0) {
  4040. dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
  4041. goto out;
  4042. }
  4043. bp->n_irqs = err;
  4044. pci_set_master(pdev);
  4045. err = ptp_ocp_register_resources(bp, id->driver_data);
  4046. if (err)
  4047. goto out;
  4048. bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
  4049. if (IS_ERR(bp->ptp)) {
  4050. err = PTR_ERR(bp->ptp);
  4051. dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
  4052. bp->ptp = NULL;
  4053. goto out;
  4054. }
  4055. err = ptp_ocp_complete(bp);
  4056. if (err)
  4057. goto out;
  4058. ptp_ocp_info(bp);
  4059. devlink_register(devlink);
  4060. clkid = pci_get_dsn(pdev);
  4061. bp->dpll = dpll_device_get(clkid, 0, THIS_MODULE, &bp->tracker);
  4062. if (IS_ERR(bp->dpll)) {
  4063. err = PTR_ERR(bp->dpll);
  4064. dev_err(&pdev->dev, "dpll_device_alloc failed\n");
  4065. goto out;
  4066. }
  4067. err = dpll_device_register(bp->dpll, DPLL_TYPE_PPS, &dpll_ops, bp);
  4068. if (err)
  4069. goto out;
  4070. for (i = 0; i < OCP_SMA_NUM; i++) {
  4071. bp->sma[i].dpll_pin = dpll_pin_get(clkid, i, THIS_MODULE,
  4072. &bp->sma[i].dpll_prop,
  4073. &bp->sma[i].tracker);
  4074. if (IS_ERR(bp->sma[i].dpll_pin)) {
  4075. err = PTR_ERR(bp->sma[i].dpll_pin);
  4076. goto out_dpll;
  4077. }
  4078. err = dpll_pin_register(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops,
  4079. &bp->sma[i]);
  4080. if (err) {
  4081. dpll_pin_put(bp->sma[i].dpll_pin, &bp->sma[i].tracker);
  4082. goto out_dpll;
  4083. }
  4084. }
  4085. queue_delayed_work(system_power_efficient_wq, &bp->sync_work, HZ);
  4086. return 0;
  4087. out_dpll:
  4088. while (i--) {
  4089. dpll_pin_unregister(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops, &bp->sma[i]);
  4090. dpll_pin_put(bp->sma[i].dpll_pin, &bp->sma[i].tracker);
  4091. }
  4092. dpll_device_put(bp->dpll, &bp->tracker);
  4093. out:
  4094. ptp_ocp_detach(bp);
  4095. out_disable:
  4096. pci_disable_device(pdev);
  4097. out_free:
  4098. devlink_free(devlink);
  4099. return err;
  4100. }
  4101. static void
  4102. ptp_ocp_remove(struct pci_dev *pdev)
  4103. {
  4104. struct ptp_ocp *bp = pci_get_drvdata(pdev);
  4105. struct devlink *devlink = priv_to_devlink(bp);
  4106. int i;
  4107. cancel_delayed_work_sync(&bp->sync_work);
  4108. for (i = 0; i < OCP_SMA_NUM; i++) {
  4109. if (bp->sma[i].dpll_pin) {
  4110. dpll_pin_unregister(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops, &bp->sma[i]);
  4111. dpll_pin_put(bp->sma[i].dpll_pin, &bp->sma[i].tracker);
  4112. }
  4113. }
  4114. dpll_device_unregister(bp->dpll, &dpll_ops, bp);
  4115. dpll_device_put(bp->dpll, &bp->tracker);
  4116. devlink_unregister(devlink);
  4117. ptp_ocp_detach(bp);
  4118. pci_disable_device(pdev);
  4119. devlink_free(devlink);
  4120. }
  4121. static struct pci_driver ptp_ocp_driver = {
  4122. .name = KBUILD_MODNAME,
  4123. .id_table = ptp_ocp_pcidev_id,
  4124. .probe = ptp_ocp_probe,
  4125. .remove = ptp_ocp_remove,
  4126. };
  4127. static int
  4128. ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
  4129. unsigned long action, void *data)
  4130. {
  4131. struct device *dev, *child = data;
  4132. struct ptp_ocp *bp;
  4133. bool add;
  4134. switch (action) {
  4135. case BUS_NOTIFY_ADD_DEVICE:
  4136. case BUS_NOTIFY_DEL_DEVICE:
  4137. add = action == BUS_NOTIFY_ADD_DEVICE;
  4138. break;
  4139. default:
  4140. return 0;
  4141. }
  4142. if (!i2c_verify_adapter(child))
  4143. return 0;
  4144. dev = child;
  4145. while ((dev = dev->parent))
  4146. if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
  4147. goto found;
  4148. return 0;
  4149. found:
  4150. bp = dev_get_drvdata(dev);
  4151. if (add)
  4152. ptp_ocp_symlink(bp, child, "i2c");
  4153. else
  4154. sysfs_remove_link(&bp->dev.kobj, "i2c");
  4155. return 0;
  4156. }
  4157. static struct notifier_block ptp_ocp_i2c_notifier = {
  4158. .notifier_call = ptp_ocp_i2c_notifier_call,
  4159. };
  4160. static int __init
  4161. ptp_ocp_init(void)
  4162. {
  4163. const char *what;
  4164. int err;
  4165. ptp_ocp_debugfs_init();
  4166. what = "timecard class";
  4167. err = class_register(&timecard_class);
  4168. if (err)
  4169. goto out;
  4170. what = "i2c notifier";
  4171. err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
  4172. if (err)
  4173. goto out_notifier;
  4174. what = "ptp_ocp driver";
  4175. err = pci_register_driver(&ptp_ocp_driver);
  4176. if (err)
  4177. goto out_register;
  4178. return 0;
  4179. out_register:
  4180. bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
  4181. out_notifier:
  4182. class_unregister(&timecard_class);
  4183. out:
  4184. ptp_ocp_debugfs_fini();
  4185. pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
  4186. return err;
  4187. }
  4188. static void __exit
  4189. ptp_ocp_fini(void)
  4190. {
  4191. bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
  4192. pci_unregister_driver(&ptp_ocp_driver);
  4193. class_unregister(&timecard_class);
  4194. ptp_ocp_debugfs_fini();
  4195. }
  4196. module_init(ptp_ocp_init);
  4197. module_exit(ptp_ocp_fini);
  4198. MODULE_DESCRIPTION("OpenCompute TimeCard driver");
  4199. MODULE_LICENSE("GPL v2");