janz-ican3.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. /*
  2. * Janz MODULbus VMOD-ICAN3 CAN Interface Driver
  3. *
  4. * Copyright (c) 2010 Ira W. Snyder <iws@ovro.caltech.edu>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/delay.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/can.h>
  18. #include <linux/can/dev.h>
  19. #include <linux/can/skb.h>
  20. #include <linux/can/error.h>
  21. #include <linux/mfd/janz.h>
  22. #include <asm/io.h>
  23. /* the DPM has 64k of memory, organized into 256x 256 byte pages */
  24. #define DPM_NUM_PAGES 256
  25. #define DPM_PAGE_SIZE 256
  26. #define DPM_PAGE_ADDR(p) ((p) * DPM_PAGE_SIZE)
  27. /* JANZ ICAN3 "old-style" host interface queue page numbers */
  28. #define QUEUE_OLD_CONTROL 0
  29. #define QUEUE_OLD_RB0 1
  30. #define QUEUE_OLD_RB1 2
  31. #define QUEUE_OLD_WB0 3
  32. #define QUEUE_OLD_WB1 4
  33. /* Janz ICAN3 "old-style" host interface control registers */
  34. #define MSYNC_PEER 0x00 /* ICAN only */
  35. #define MSYNC_LOCL 0x01 /* host only */
  36. #define TARGET_RUNNING 0x02
  37. #define MSYNC_RB0 0x01
  38. #define MSYNC_RB1 0x02
  39. #define MSYNC_RBLW 0x04
  40. #define MSYNC_RB_MASK (MSYNC_RB0 | MSYNC_RB1)
  41. #define MSYNC_WB0 0x10
  42. #define MSYNC_WB1 0x20
  43. #define MSYNC_WBLW 0x40
  44. #define MSYNC_WB_MASK (MSYNC_WB0 | MSYNC_WB1)
  45. /* Janz ICAN3 "new-style" host interface queue page numbers */
  46. #define QUEUE_TOHOST 5
  47. #define QUEUE_FROMHOST_MID 6
  48. #define QUEUE_FROMHOST_HIGH 7
  49. #define QUEUE_FROMHOST_LOW 8
  50. /* The first free page in the DPM is #9 */
  51. #define DPM_FREE_START 9
  52. /* Janz ICAN3 "new-style" and "fast" host interface descriptor flags */
  53. #define DESC_VALID 0x80
  54. #define DESC_WRAP 0x40
  55. #define DESC_INTERRUPT 0x20
  56. #define DESC_IVALID 0x10
  57. #define DESC_LEN(len) (len)
  58. /* Janz ICAN3 Firmware Messages */
  59. #define MSG_CONNECTI 0x02
  60. #define MSG_DISCONNECT 0x03
  61. #define MSG_IDVERS 0x04
  62. #define MSG_MSGLOST 0x05
  63. #define MSG_NEWHOSTIF 0x08
  64. #define MSG_INQUIRY 0x0a
  65. #define MSG_SETAFILMASK 0x10
  66. #define MSG_INITFDPMQUEUE 0x11
  67. #define MSG_HWCONF 0x12
  68. #define MSG_FMSGLOST 0x15
  69. #define MSG_CEVTIND 0x37
  70. #define MSG_CBTRREQ 0x41
  71. #define MSG_COFFREQ 0x42
  72. #define MSG_CONREQ 0x43
  73. #define MSG_CCONFREQ 0x47
  74. /*
  75. * Janz ICAN3 CAN Inquiry Message Types
  76. *
  77. * NOTE: there appears to be a firmware bug here. You must send
  78. * NOTE: INQUIRY_STATUS and expect to receive an INQUIRY_EXTENDED
  79. * NOTE: response. The controller never responds to a message with
  80. * NOTE: the INQUIRY_EXTENDED subspec :(
  81. */
  82. #define INQUIRY_STATUS 0x00
  83. #define INQUIRY_TERMINATION 0x01
  84. #define INQUIRY_EXTENDED 0x04
  85. /* Janz ICAN3 CAN Set Acceptance Filter Mask Message Types */
  86. #define SETAFILMASK_REJECT 0x00
  87. #define SETAFILMASK_FASTIF 0x02
  88. /* Janz ICAN3 CAN Hardware Configuration Message Types */
  89. #define HWCONF_TERMINATE_ON 0x01
  90. #define HWCONF_TERMINATE_OFF 0x00
  91. /* Janz ICAN3 CAN Event Indication Message Types */
  92. #define CEVTIND_EI 0x01
  93. #define CEVTIND_DOI 0x02
  94. #define CEVTIND_LOST 0x04
  95. #define CEVTIND_FULL 0x08
  96. #define CEVTIND_BEI 0x10
  97. #define CEVTIND_CHIP_SJA1000 0x02
  98. #define ICAN3_BUSERR_QUOTA_MAX 255
  99. /* Janz ICAN3 CAN Frame Conversion */
  100. #define ICAN3_SNGL 0x02
  101. #define ICAN3_ECHO 0x10
  102. #define ICAN3_EFF_RTR 0x40
  103. #define ICAN3_SFF_RTR 0x10
  104. #define ICAN3_EFF 0x80
  105. #define ICAN3_CAN_TYPE_MASK 0x0f
  106. #define ICAN3_CAN_TYPE_SFF 0x00
  107. #define ICAN3_CAN_TYPE_EFF 0x01
  108. #define ICAN3_CAN_DLC_MASK 0x0f
  109. /*
  110. * SJA1000 Status and Error Register Definitions
  111. *
  112. * Copied from drivers/net/can/sja1000/sja1000.h
  113. */
  114. /* status register content */
  115. #define SR_BS 0x80
  116. #define SR_ES 0x40
  117. #define SR_TS 0x20
  118. #define SR_RS 0x10
  119. #define SR_TCS 0x08
  120. #define SR_TBS 0x04
  121. #define SR_DOS 0x02
  122. #define SR_RBS 0x01
  123. #define SR_CRIT (SR_BS|SR_ES)
  124. /* ECC register */
  125. #define ECC_SEG 0x1F
  126. #define ECC_DIR 0x20
  127. #define ECC_ERR 6
  128. #define ECC_BIT 0x00
  129. #define ECC_FORM 0x40
  130. #define ECC_STUFF 0x80
  131. #define ECC_MASK 0xc0
  132. /* Number of buffers for use in the "new-style" host interface */
  133. #define ICAN3_NEW_BUFFERS 16
  134. /* Number of buffers for use in the "fast" host interface */
  135. #define ICAN3_TX_BUFFERS 512
  136. #define ICAN3_RX_BUFFERS 1024
  137. /* SJA1000 Clock Input */
  138. #define ICAN3_CAN_CLOCK 8000000
  139. /* Driver Name */
  140. #define DRV_NAME "janz-ican3"
  141. /* DPM Control Registers -- starts at offset 0x100 in the MODULbus registers */
  142. struct ican3_dpm_control {
  143. /* window address register */
  144. u8 window_address;
  145. u8 unused1;
  146. /*
  147. * Read access: clear interrupt from microcontroller
  148. * Write access: send interrupt to microcontroller
  149. */
  150. u8 interrupt;
  151. u8 unused2;
  152. /* write-only: reset all hardware on the module */
  153. u8 hwreset;
  154. u8 unused3;
  155. /* write-only: generate an interrupt to the TPU */
  156. u8 tpuinterrupt;
  157. };
  158. struct ican3_dev {
  159. /* must be the first member */
  160. struct can_priv can;
  161. /* CAN network device */
  162. struct net_device *ndev;
  163. struct napi_struct napi;
  164. /* module number */
  165. unsigned int num;
  166. /* base address of registers and IRQ */
  167. struct janz_cmodio_onboard_regs __iomem *ctrl;
  168. struct ican3_dpm_control __iomem *dpmctrl;
  169. void __iomem *dpm;
  170. int irq;
  171. /* CAN bus termination status */
  172. struct completion termination_comp;
  173. bool termination_enabled;
  174. /* CAN bus error status registers */
  175. struct completion buserror_comp;
  176. struct can_berr_counter bec;
  177. /* old and new style host interface */
  178. unsigned int iftype;
  179. /* queue for echo packets */
  180. struct sk_buff_head echoq;
  181. /*
  182. * Any function which changes the current DPM page must hold this
  183. * lock while it is performing data accesses. This ensures that the
  184. * function will not be preempted and end up reading data from a
  185. * different DPM page than it expects.
  186. */
  187. spinlock_t lock;
  188. /* new host interface */
  189. unsigned int rx_int;
  190. unsigned int rx_num;
  191. unsigned int tx_num;
  192. /* fast host interface */
  193. unsigned int fastrx_start;
  194. unsigned int fastrx_num;
  195. unsigned int fasttx_start;
  196. unsigned int fasttx_num;
  197. /* first free DPM page */
  198. unsigned int free_page;
  199. };
  200. struct ican3_msg {
  201. u8 control;
  202. u8 spec;
  203. __le16 len;
  204. u8 data[252];
  205. };
  206. struct ican3_new_desc {
  207. u8 control;
  208. u8 pointer;
  209. };
  210. struct ican3_fast_desc {
  211. u8 control;
  212. u8 command;
  213. u8 data[14];
  214. };
  215. /* write to the window basic address register */
  216. static inline void ican3_set_page(struct ican3_dev *mod, unsigned int page)
  217. {
  218. BUG_ON(page >= DPM_NUM_PAGES);
  219. iowrite8(page, &mod->dpmctrl->window_address);
  220. }
  221. /*
  222. * ICAN3 "old-style" host interface
  223. */
  224. /*
  225. * Receive a message from the ICAN3 "old-style" firmware interface
  226. *
  227. * LOCKING: must hold mod->lock
  228. *
  229. * returns 0 on success, -ENOMEM when no message exists
  230. */
  231. static int ican3_old_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  232. {
  233. unsigned int mbox, mbox_page;
  234. u8 locl, peer, xord;
  235. /* get the MSYNC registers */
  236. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  237. peer = ioread8(mod->dpm + MSYNC_PEER);
  238. locl = ioread8(mod->dpm + MSYNC_LOCL);
  239. xord = locl ^ peer;
  240. if ((xord & MSYNC_RB_MASK) == 0x00) {
  241. netdev_dbg(mod->ndev, "no mbox for reading\n");
  242. return -ENOMEM;
  243. }
  244. /* find the first free mbox to read */
  245. if ((xord & MSYNC_RB_MASK) == MSYNC_RB_MASK)
  246. mbox = (xord & MSYNC_RBLW) ? MSYNC_RB0 : MSYNC_RB1;
  247. else
  248. mbox = (xord & MSYNC_RB0) ? MSYNC_RB0 : MSYNC_RB1;
  249. /* copy the message */
  250. mbox_page = (mbox == MSYNC_RB0) ? QUEUE_OLD_RB0 : QUEUE_OLD_RB1;
  251. ican3_set_page(mod, mbox_page);
  252. memcpy_fromio(msg, mod->dpm, sizeof(*msg));
  253. /*
  254. * notify the firmware that the read buffer is available
  255. * for it to fill again
  256. */
  257. locl ^= mbox;
  258. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  259. iowrite8(locl, mod->dpm + MSYNC_LOCL);
  260. return 0;
  261. }
  262. /*
  263. * Send a message through the "old-style" firmware interface
  264. *
  265. * LOCKING: must hold mod->lock
  266. *
  267. * returns 0 on success, -ENOMEM when no free space exists
  268. */
  269. static int ican3_old_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  270. {
  271. unsigned int mbox, mbox_page;
  272. u8 locl, peer, xord;
  273. /* get the MSYNC registers */
  274. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  275. peer = ioread8(mod->dpm + MSYNC_PEER);
  276. locl = ioread8(mod->dpm + MSYNC_LOCL);
  277. xord = locl ^ peer;
  278. if ((xord & MSYNC_WB_MASK) == MSYNC_WB_MASK) {
  279. netdev_err(mod->ndev, "no mbox for writing\n");
  280. return -ENOMEM;
  281. }
  282. /* calculate a free mbox to use */
  283. mbox = (xord & MSYNC_WB0) ? MSYNC_WB1 : MSYNC_WB0;
  284. /* copy the message to the DPM */
  285. mbox_page = (mbox == MSYNC_WB0) ? QUEUE_OLD_WB0 : QUEUE_OLD_WB1;
  286. ican3_set_page(mod, mbox_page);
  287. memcpy_toio(mod->dpm, msg, sizeof(*msg));
  288. locl ^= mbox;
  289. if (mbox == MSYNC_WB1)
  290. locl |= MSYNC_WBLW;
  291. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  292. iowrite8(locl, mod->dpm + MSYNC_LOCL);
  293. return 0;
  294. }
  295. /*
  296. * ICAN3 "new-style" Host Interface Setup
  297. */
  298. static void ican3_init_new_host_interface(struct ican3_dev *mod)
  299. {
  300. struct ican3_new_desc desc;
  301. unsigned long flags;
  302. void __iomem *dst;
  303. int i;
  304. spin_lock_irqsave(&mod->lock, flags);
  305. /* setup the internal datastructures for RX */
  306. mod->rx_num = 0;
  307. mod->rx_int = 0;
  308. /* tohost queue descriptors are in page 5 */
  309. ican3_set_page(mod, QUEUE_TOHOST);
  310. dst = mod->dpm;
  311. /* initialize the tohost (rx) queue descriptors: pages 9-24 */
  312. for (i = 0; i < ICAN3_NEW_BUFFERS; i++) {
  313. desc.control = DESC_INTERRUPT | DESC_LEN(1); /* I L=1 */
  314. desc.pointer = mod->free_page;
  315. /* set wrap flag on last buffer */
  316. if (i == ICAN3_NEW_BUFFERS - 1)
  317. desc.control |= DESC_WRAP;
  318. memcpy_toio(dst, &desc, sizeof(desc));
  319. dst += sizeof(desc);
  320. mod->free_page++;
  321. }
  322. /* fromhost (tx) mid queue descriptors are in page 6 */
  323. ican3_set_page(mod, QUEUE_FROMHOST_MID);
  324. dst = mod->dpm;
  325. /* setup the internal datastructures for TX */
  326. mod->tx_num = 0;
  327. /* initialize the fromhost mid queue descriptors: pages 25-40 */
  328. for (i = 0; i < ICAN3_NEW_BUFFERS; i++) {
  329. desc.control = DESC_VALID | DESC_LEN(1); /* V L=1 */
  330. desc.pointer = mod->free_page;
  331. /* set wrap flag on last buffer */
  332. if (i == ICAN3_NEW_BUFFERS - 1)
  333. desc.control |= DESC_WRAP;
  334. memcpy_toio(dst, &desc, sizeof(desc));
  335. dst += sizeof(desc);
  336. mod->free_page++;
  337. }
  338. /* fromhost hi queue descriptors are in page 7 */
  339. ican3_set_page(mod, QUEUE_FROMHOST_HIGH);
  340. dst = mod->dpm;
  341. /* initialize only a single buffer in the fromhost hi queue (unused) */
  342. desc.control = DESC_VALID | DESC_WRAP | DESC_LEN(1); /* VW L=1 */
  343. desc.pointer = mod->free_page;
  344. memcpy_toio(dst, &desc, sizeof(desc));
  345. mod->free_page++;
  346. /* fromhost low queue descriptors are in page 8 */
  347. ican3_set_page(mod, QUEUE_FROMHOST_LOW);
  348. dst = mod->dpm;
  349. /* initialize only a single buffer in the fromhost low queue (unused) */
  350. desc.control = DESC_VALID | DESC_WRAP | DESC_LEN(1); /* VW L=1 */
  351. desc.pointer = mod->free_page;
  352. memcpy_toio(dst, &desc, sizeof(desc));
  353. mod->free_page++;
  354. spin_unlock_irqrestore(&mod->lock, flags);
  355. }
  356. /*
  357. * ICAN3 Fast Host Interface Setup
  358. */
  359. static void ican3_init_fast_host_interface(struct ican3_dev *mod)
  360. {
  361. struct ican3_fast_desc desc;
  362. unsigned long flags;
  363. unsigned int addr;
  364. void __iomem *dst;
  365. int i;
  366. spin_lock_irqsave(&mod->lock, flags);
  367. /* save the start recv page */
  368. mod->fastrx_start = mod->free_page;
  369. mod->fastrx_num = 0;
  370. /* build a single fast tohost queue descriptor */
  371. memset(&desc, 0, sizeof(desc));
  372. desc.control = 0x00;
  373. desc.command = 1;
  374. /* build the tohost queue descriptor ring in memory */
  375. addr = 0;
  376. for (i = 0; i < ICAN3_RX_BUFFERS; i++) {
  377. /* set the wrap bit on the last buffer */
  378. if (i == ICAN3_RX_BUFFERS - 1)
  379. desc.control |= DESC_WRAP;
  380. /* switch to the correct page */
  381. ican3_set_page(mod, mod->free_page);
  382. /* copy the descriptor to the DPM */
  383. dst = mod->dpm + addr;
  384. memcpy_toio(dst, &desc, sizeof(desc));
  385. addr += sizeof(desc);
  386. /* move to the next page if necessary */
  387. if (addr >= DPM_PAGE_SIZE) {
  388. addr = 0;
  389. mod->free_page++;
  390. }
  391. }
  392. /* make sure we page-align the next queue */
  393. if (addr != 0)
  394. mod->free_page++;
  395. /* save the start xmit page */
  396. mod->fasttx_start = mod->free_page;
  397. mod->fasttx_num = 0;
  398. /* build a single fast fromhost queue descriptor */
  399. memset(&desc, 0, sizeof(desc));
  400. desc.control = DESC_VALID;
  401. desc.command = 1;
  402. /* build the fromhost queue descriptor ring in memory */
  403. addr = 0;
  404. for (i = 0; i < ICAN3_TX_BUFFERS; i++) {
  405. /* set the wrap bit on the last buffer */
  406. if (i == ICAN3_TX_BUFFERS - 1)
  407. desc.control |= DESC_WRAP;
  408. /* switch to the correct page */
  409. ican3_set_page(mod, mod->free_page);
  410. /* copy the descriptor to the DPM */
  411. dst = mod->dpm + addr;
  412. memcpy_toio(dst, &desc, sizeof(desc));
  413. addr += sizeof(desc);
  414. /* move to the next page if necessary */
  415. if (addr >= DPM_PAGE_SIZE) {
  416. addr = 0;
  417. mod->free_page++;
  418. }
  419. }
  420. spin_unlock_irqrestore(&mod->lock, flags);
  421. }
  422. /*
  423. * ICAN3 "new-style" Host Interface Message Helpers
  424. */
  425. /*
  426. * LOCKING: must hold mod->lock
  427. */
  428. static int ican3_new_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  429. {
  430. struct ican3_new_desc desc;
  431. void __iomem *desc_addr = mod->dpm + (mod->tx_num * sizeof(desc));
  432. /* switch to the fromhost mid queue, and read the buffer descriptor */
  433. ican3_set_page(mod, QUEUE_FROMHOST_MID);
  434. memcpy_fromio(&desc, desc_addr, sizeof(desc));
  435. if (!(desc.control & DESC_VALID)) {
  436. netdev_dbg(mod->ndev, "%s: no free buffers\n", __func__);
  437. return -ENOMEM;
  438. }
  439. /* switch to the data page, copy the data */
  440. ican3_set_page(mod, desc.pointer);
  441. memcpy_toio(mod->dpm, msg, sizeof(*msg));
  442. /* switch back to the descriptor, set the valid bit, write it back */
  443. ican3_set_page(mod, QUEUE_FROMHOST_MID);
  444. desc.control ^= DESC_VALID;
  445. memcpy_toio(desc_addr, &desc, sizeof(desc));
  446. /* update the tx number */
  447. mod->tx_num = (desc.control & DESC_WRAP) ? 0 : (mod->tx_num + 1);
  448. return 0;
  449. }
  450. /*
  451. * LOCKING: must hold mod->lock
  452. */
  453. static int ican3_new_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  454. {
  455. struct ican3_new_desc desc;
  456. void __iomem *desc_addr = mod->dpm + (mod->rx_num * sizeof(desc));
  457. /* switch to the tohost queue, and read the buffer descriptor */
  458. ican3_set_page(mod, QUEUE_TOHOST);
  459. memcpy_fromio(&desc, desc_addr, sizeof(desc));
  460. if (!(desc.control & DESC_VALID)) {
  461. netdev_dbg(mod->ndev, "%s: no buffers to recv\n", __func__);
  462. return -ENOMEM;
  463. }
  464. /* switch to the data page, copy the data */
  465. ican3_set_page(mod, desc.pointer);
  466. memcpy_fromio(msg, mod->dpm, sizeof(*msg));
  467. /* switch back to the descriptor, toggle the valid bit, write it back */
  468. ican3_set_page(mod, QUEUE_TOHOST);
  469. desc.control ^= DESC_VALID;
  470. memcpy_toio(desc_addr, &desc, sizeof(desc));
  471. /* update the rx number */
  472. mod->rx_num = (desc.control & DESC_WRAP) ? 0 : (mod->rx_num + 1);
  473. return 0;
  474. }
  475. /*
  476. * Message Send / Recv Helpers
  477. */
  478. static int ican3_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  479. {
  480. unsigned long flags;
  481. int ret;
  482. spin_lock_irqsave(&mod->lock, flags);
  483. if (mod->iftype == 0)
  484. ret = ican3_old_send_msg(mod, msg);
  485. else
  486. ret = ican3_new_send_msg(mod, msg);
  487. spin_unlock_irqrestore(&mod->lock, flags);
  488. return ret;
  489. }
  490. static int ican3_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  491. {
  492. unsigned long flags;
  493. int ret;
  494. spin_lock_irqsave(&mod->lock, flags);
  495. if (mod->iftype == 0)
  496. ret = ican3_old_recv_msg(mod, msg);
  497. else
  498. ret = ican3_new_recv_msg(mod, msg);
  499. spin_unlock_irqrestore(&mod->lock, flags);
  500. return ret;
  501. }
  502. /*
  503. * Quick Pre-constructed Messages
  504. */
  505. static int ican3_msg_connect(struct ican3_dev *mod)
  506. {
  507. struct ican3_msg msg;
  508. memset(&msg, 0, sizeof(msg));
  509. msg.spec = MSG_CONNECTI;
  510. msg.len = cpu_to_le16(0);
  511. return ican3_send_msg(mod, &msg);
  512. }
  513. static int ican3_msg_disconnect(struct ican3_dev *mod)
  514. {
  515. struct ican3_msg msg;
  516. memset(&msg, 0, sizeof(msg));
  517. msg.spec = MSG_DISCONNECT;
  518. msg.len = cpu_to_le16(0);
  519. return ican3_send_msg(mod, &msg);
  520. }
  521. static int ican3_msg_newhostif(struct ican3_dev *mod)
  522. {
  523. struct ican3_msg msg;
  524. int ret;
  525. memset(&msg, 0, sizeof(msg));
  526. msg.spec = MSG_NEWHOSTIF;
  527. msg.len = cpu_to_le16(0);
  528. /* If we're not using the old interface, switching seems bogus */
  529. WARN_ON(mod->iftype != 0);
  530. ret = ican3_send_msg(mod, &msg);
  531. if (ret)
  532. return ret;
  533. /* mark the module as using the new host interface */
  534. mod->iftype = 1;
  535. return 0;
  536. }
  537. static int ican3_msg_fasthostif(struct ican3_dev *mod)
  538. {
  539. struct ican3_msg msg;
  540. unsigned int addr;
  541. memset(&msg, 0, sizeof(msg));
  542. msg.spec = MSG_INITFDPMQUEUE;
  543. msg.len = cpu_to_le16(8);
  544. /* write the tohost queue start address */
  545. addr = DPM_PAGE_ADDR(mod->fastrx_start);
  546. msg.data[0] = addr & 0xff;
  547. msg.data[1] = (addr >> 8) & 0xff;
  548. msg.data[2] = (addr >> 16) & 0xff;
  549. msg.data[3] = (addr >> 24) & 0xff;
  550. /* write the fromhost queue start address */
  551. addr = DPM_PAGE_ADDR(mod->fasttx_start);
  552. msg.data[4] = addr & 0xff;
  553. msg.data[5] = (addr >> 8) & 0xff;
  554. msg.data[6] = (addr >> 16) & 0xff;
  555. msg.data[7] = (addr >> 24) & 0xff;
  556. /* If we're not using the new interface yet, we cannot do this */
  557. WARN_ON(mod->iftype != 1);
  558. return ican3_send_msg(mod, &msg);
  559. }
  560. /*
  561. * Setup the CAN filter to either accept or reject all
  562. * messages from the CAN bus.
  563. */
  564. static int ican3_set_id_filter(struct ican3_dev *mod, bool accept)
  565. {
  566. struct ican3_msg msg;
  567. int ret;
  568. /* Standard Frame Format */
  569. memset(&msg, 0, sizeof(msg));
  570. msg.spec = MSG_SETAFILMASK;
  571. msg.len = cpu_to_le16(5);
  572. msg.data[0] = 0x00; /* IDLo LSB */
  573. msg.data[1] = 0x00; /* IDLo MSB */
  574. msg.data[2] = 0xff; /* IDHi LSB */
  575. msg.data[3] = 0x07; /* IDHi MSB */
  576. /* accept all frames for fast host if, or reject all frames */
  577. msg.data[4] = accept ? SETAFILMASK_FASTIF : SETAFILMASK_REJECT;
  578. ret = ican3_send_msg(mod, &msg);
  579. if (ret)
  580. return ret;
  581. /* Extended Frame Format */
  582. memset(&msg, 0, sizeof(msg));
  583. msg.spec = MSG_SETAFILMASK;
  584. msg.len = cpu_to_le16(13);
  585. msg.data[0] = 0; /* MUX = 0 */
  586. msg.data[1] = 0x00; /* IDLo LSB */
  587. msg.data[2] = 0x00;
  588. msg.data[3] = 0x00;
  589. msg.data[4] = 0x20; /* IDLo MSB */
  590. msg.data[5] = 0xff; /* IDHi LSB */
  591. msg.data[6] = 0xff;
  592. msg.data[7] = 0xff;
  593. msg.data[8] = 0x3f; /* IDHi MSB */
  594. /* accept all frames for fast host if, or reject all frames */
  595. msg.data[9] = accept ? SETAFILMASK_FASTIF : SETAFILMASK_REJECT;
  596. return ican3_send_msg(mod, &msg);
  597. }
  598. /*
  599. * Bring the CAN bus online or offline
  600. */
  601. static int ican3_set_bus_state(struct ican3_dev *mod, bool on)
  602. {
  603. struct ican3_msg msg;
  604. memset(&msg, 0, sizeof(msg));
  605. msg.spec = on ? MSG_CONREQ : MSG_COFFREQ;
  606. msg.len = cpu_to_le16(0);
  607. return ican3_send_msg(mod, &msg);
  608. }
  609. static int ican3_set_termination(struct ican3_dev *mod, bool on)
  610. {
  611. struct ican3_msg msg;
  612. memset(&msg, 0, sizeof(msg));
  613. msg.spec = MSG_HWCONF;
  614. msg.len = cpu_to_le16(2);
  615. msg.data[0] = 0x00;
  616. msg.data[1] = on ? HWCONF_TERMINATE_ON : HWCONF_TERMINATE_OFF;
  617. return ican3_send_msg(mod, &msg);
  618. }
  619. static int ican3_send_inquiry(struct ican3_dev *mod, u8 subspec)
  620. {
  621. struct ican3_msg msg;
  622. memset(&msg, 0, sizeof(msg));
  623. msg.spec = MSG_INQUIRY;
  624. msg.len = cpu_to_le16(2);
  625. msg.data[0] = subspec;
  626. msg.data[1] = 0x00;
  627. return ican3_send_msg(mod, &msg);
  628. }
  629. static int ican3_set_buserror(struct ican3_dev *mod, u8 quota)
  630. {
  631. struct ican3_msg msg;
  632. memset(&msg, 0, sizeof(msg));
  633. msg.spec = MSG_CCONFREQ;
  634. msg.len = cpu_to_le16(2);
  635. msg.data[0] = 0x00;
  636. msg.data[1] = quota;
  637. return ican3_send_msg(mod, &msg);
  638. }
  639. /*
  640. * ICAN3 to Linux CAN Frame Conversion
  641. */
  642. static void ican3_to_can_frame(struct ican3_dev *mod,
  643. struct ican3_fast_desc *desc,
  644. struct can_frame *cf)
  645. {
  646. if ((desc->command & ICAN3_CAN_TYPE_MASK) == ICAN3_CAN_TYPE_SFF) {
  647. if (desc->data[1] & ICAN3_SFF_RTR)
  648. cf->can_id |= CAN_RTR_FLAG;
  649. cf->can_id |= desc->data[0] << 3;
  650. cf->can_id |= (desc->data[1] & 0xe0) >> 5;
  651. cf->can_dlc = get_can_dlc(desc->data[1] & ICAN3_CAN_DLC_MASK);
  652. memcpy(cf->data, &desc->data[2], cf->can_dlc);
  653. } else {
  654. cf->can_dlc = get_can_dlc(desc->data[0] & ICAN3_CAN_DLC_MASK);
  655. if (desc->data[0] & ICAN3_EFF_RTR)
  656. cf->can_id |= CAN_RTR_FLAG;
  657. if (desc->data[0] & ICAN3_EFF) {
  658. cf->can_id |= CAN_EFF_FLAG;
  659. cf->can_id |= desc->data[2] << 21; /* 28-21 */
  660. cf->can_id |= desc->data[3] << 13; /* 20-13 */
  661. cf->can_id |= desc->data[4] << 5; /* 12-5 */
  662. cf->can_id |= (desc->data[5] & 0xf8) >> 3;
  663. } else {
  664. cf->can_id |= desc->data[2] << 3; /* 10-3 */
  665. cf->can_id |= desc->data[3] >> 5; /* 2-0 */
  666. }
  667. memcpy(cf->data, &desc->data[6], cf->can_dlc);
  668. }
  669. }
  670. static void can_frame_to_ican3(struct ican3_dev *mod,
  671. struct can_frame *cf,
  672. struct ican3_fast_desc *desc)
  673. {
  674. /* clear out any stale data in the descriptor */
  675. memset(desc->data, 0, sizeof(desc->data));
  676. /* we always use the extended format, with the ECHO flag set */
  677. desc->command = ICAN3_CAN_TYPE_EFF;
  678. desc->data[0] |= cf->can_dlc;
  679. desc->data[1] |= ICAN3_ECHO;
  680. /* support single transmission (no retries) mode */
  681. if (mod->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  682. desc->data[1] |= ICAN3_SNGL;
  683. if (cf->can_id & CAN_RTR_FLAG)
  684. desc->data[0] |= ICAN3_EFF_RTR;
  685. /* pack the id into the correct places */
  686. if (cf->can_id & CAN_EFF_FLAG) {
  687. desc->data[0] |= ICAN3_EFF;
  688. desc->data[2] = (cf->can_id & 0x1fe00000) >> 21; /* 28-21 */
  689. desc->data[3] = (cf->can_id & 0x001fe000) >> 13; /* 20-13 */
  690. desc->data[4] = (cf->can_id & 0x00001fe0) >> 5; /* 12-5 */
  691. desc->data[5] = (cf->can_id & 0x0000001f) << 3; /* 4-0 */
  692. } else {
  693. desc->data[2] = (cf->can_id & 0x7F8) >> 3; /* bits 10-3 */
  694. desc->data[3] = (cf->can_id & 0x007) << 5; /* bits 2-0 */
  695. }
  696. /* copy the data bits into the descriptor */
  697. memcpy(&desc->data[6], cf->data, cf->can_dlc);
  698. }
  699. /*
  700. * Interrupt Handling
  701. */
  702. /*
  703. * Handle an ID + Version message response from the firmware. We never generate
  704. * this message in production code, but it is very useful when debugging to be
  705. * able to display this message.
  706. */
  707. static void ican3_handle_idvers(struct ican3_dev *mod, struct ican3_msg *msg)
  708. {
  709. netdev_dbg(mod->ndev, "IDVERS response: %s\n", msg->data);
  710. }
  711. static void ican3_handle_msglost(struct ican3_dev *mod, struct ican3_msg *msg)
  712. {
  713. struct net_device *dev = mod->ndev;
  714. struct net_device_stats *stats = &dev->stats;
  715. struct can_frame *cf;
  716. struct sk_buff *skb;
  717. /*
  718. * Report that communication messages with the microcontroller firmware
  719. * are being lost. These are never CAN frames, so we do not generate an
  720. * error frame for userspace
  721. */
  722. if (msg->spec == MSG_MSGLOST) {
  723. netdev_err(mod->ndev, "lost %d control messages\n", msg->data[0]);
  724. return;
  725. }
  726. /*
  727. * Oops, this indicates that we have lost messages in the fast queue,
  728. * which are exclusively CAN messages. Our driver isn't reading CAN
  729. * frames fast enough.
  730. *
  731. * We'll pretend that the SJA1000 told us that it ran out of buffer
  732. * space, because there is not a better message for this.
  733. */
  734. skb = alloc_can_err_skb(dev, &cf);
  735. if (skb) {
  736. cf->can_id |= CAN_ERR_CRTL;
  737. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  738. stats->rx_over_errors++;
  739. stats->rx_errors++;
  740. netif_rx(skb);
  741. }
  742. }
  743. /*
  744. * Handle CAN Event Indication Messages from the firmware
  745. *
  746. * The ICAN3 firmware provides the values of some SJA1000 registers when it
  747. * generates this message. The code below is largely copied from the
  748. * drivers/net/can/sja1000/sja1000.c file, and adapted as necessary
  749. */
  750. static int ican3_handle_cevtind(struct ican3_dev *mod, struct ican3_msg *msg)
  751. {
  752. struct net_device *dev = mod->ndev;
  753. struct net_device_stats *stats = &dev->stats;
  754. enum can_state state = mod->can.state;
  755. u8 isrc, ecc, status, rxerr, txerr;
  756. struct can_frame *cf;
  757. struct sk_buff *skb;
  758. /* we can only handle the SJA1000 part */
  759. if (msg->data[1] != CEVTIND_CHIP_SJA1000) {
  760. netdev_err(mod->ndev, "unable to handle errors on non-SJA1000\n");
  761. return -ENODEV;
  762. }
  763. /* check the message length for sanity */
  764. if (le16_to_cpu(msg->len) < 6) {
  765. netdev_err(mod->ndev, "error message too short\n");
  766. return -EINVAL;
  767. }
  768. isrc = msg->data[0];
  769. ecc = msg->data[2];
  770. status = msg->data[3];
  771. rxerr = msg->data[4];
  772. txerr = msg->data[5];
  773. /*
  774. * This hardware lacks any support other than bus error messages to
  775. * determine if packet transmission has failed.
  776. *
  777. * When TX errors happen, one echo skb needs to be dropped from the
  778. * front of the queue.
  779. *
  780. * A small bit of code is duplicated here and below, to avoid error
  781. * skb allocation when it will just be freed immediately.
  782. */
  783. if (isrc == CEVTIND_BEI) {
  784. int ret;
  785. netdev_dbg(mod->ndev, "bus error interrupt\n");
  786. /* TX error */
  787. if (!(ecc & ECC_DIR)) {
  788. kfree_skb(skb_dequeue(&mod->echoq));
  789. stats->tx_errors++;
  790. } else {
  791. stats->rx_errors++;
  792. }
  793. /*
  794. * The controller automatically disables bus-error interrupts
  795. * and therefore we must re-enable them.
  796. */
  797. ret = ican3_set_buserror(mod, 1);
  798. if (ret) {
  799. netdev_err(mod->ndev, "unable to re-enable bus-error\n");
  800. return ret;
  801. }
  802. /* bus error reporting is off, return immediately */
  803. if (!(mod->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
  804. return 0;
  805. }
  806. skb = alloc_can_err_skb(dev, &cf);
  807. if (skb == NULL)
  808. return -ENOMEM;
  809. /* data overrun interrupt */
  810. if (isrc == CEVTIND_DOI || isrc == CEVTIND_LOST) {
  811. netdev_dbg(mod->ndev, "data overrun interrupt\n");
  812. cf->can_id |= CAN_ERR_CRTL;
  813. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  814. stats->rx_over_errors++;
  815. stats->rx_errors++;
  816. }
  817. /* error warning + passive interrupt */
  818. if (isrc == CEVTIND_EI) {
  819. netdev_dbg(mod->ndev, "error warning + passive interrupt\n");
  820. if (status & SR_BS) {
  821. state = CAN_STATE_BUS_OFF;
  822. cf->can_id |= CAN_ERR_BUSOFF;
  823. mod->can.can_stats.bus_off++;
  824. can_bus_off(dev);
  825. } else if (status & SR_ES) {
  826. if (rxerr >= 128 || txerr >= 128)
  827. state = CAN_STATE_ERROR_PASSIVE;
  828. else
  829. state = CAN_STATE_ERROR_WARNING;
  830. } else {
  831. state = CAN_STATE_ERROR_ACTIVE;
  832. }
  833. }
  834. /* bus error interrupt */
  835. if (isrc == CEVTIND_BEI) {
  836. mod->can.can_stats.bus_error++;
  837. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  838. switch (ecc & ECC_MASK) {
  839. case ECC_BIT:
  840. cf->data[2] |= CAN_ERR_PROT_BIT;
  841. break;
  842. case ECC_FORM:
  843. cf->data[2] |= CAN_ERR_PROT_FORM;
  844. break;
  845. case ECC_STUFF:
  846. cf->data[2] |= CAN_ERR_PROT_STUFF;
  847. break;
  848. default:
  849. cf->data[2] |= CAN_ERR_PROT_UNSPEC;
  850. cf->data[3] = ecc & ECC_SEG;
  851. break;
  852. }
  853. if (!(ecc & ECC_DIR))
  854. cf->data[2] |= CAN_ERR_PROT_TX;
  855. cf->data[6] = txerr;
  856. cf->data[7] = rxerr;
  857. }
  858. if (state != mod->can.state && (state == CAN_STATE_ERROR_WARNING ||
  859. state == CAN_STATE_ERROR_PASSIVE)) {
  860. cf->can_id |= CAN_ERR_CRTL;
  861. if (state == CAN_STATE_ERROR_WARNING) {
  862. mod->can.can_stats.error_warning++;
  863. cf->data[1] = (txerr > rxerr) ?
  864. CAN_ERR_CRTL_TX_WARNING :
  865. CAN_ERR_CRTL_RX_WARNING;
  866. } else {
  867. mod->can.can_stats.error_passive++;
  868. cf->data[1] = (txerr > rxerr) ?
  869. CAN_ERR_CRTL_TX_PASSIVE :
  870. CAN_ERR_CRTL_RX_PASSIVE;
  871. }
  872. cf->data[6] = txerr;
  873. cf->data[7] = rxerr;
  874. }
  875. mod->can.state = state;
  876. netif_rx(skb);
  877. return 0;
  878. }
  879. static void ican3_handle_inquiry(struct ican3_dev *mod, struct ican3_msg *msg)
  880. {
  881. switch (msg->data[0]) {
  882. case INQUIRY_STATUS:
  883. case INQUIRY_EXTENDED:
  884. mod->bec.rxerr = msg->data[5];
  885. mod->bec.txerr = msg->data[6];
  886. complete(&mod->buserror_comp);
  887. break;
  888. case INQUIRY_TERMINATION:
  889. mod->termination_enabled = msg->data[6] & HWCONF_TERMINATE_ON;
  890. complete(&mod->termination_comp);
  891. break;
  892. default:
  893. netdev_err(mod->ndev, "received an unknown inquiry response\n");
  894. break;
  895. }
  896. }
  897. static void ican3_handle_unknown_message(struct ican3_dev *mod,
  898. struct ican3_msg *msg)
  899. {
  900. netdev_warn(mod->ndev, "received unknown message: spec 0x%.2x length %d\n",
  901. msg->spec, le16_to_cpu(msg->len));
  902. }
  903. /*
  904. * Handle a control message from the firmware
  905. */
  906. static void ican3_handle_message(struct ican3_dev *mod, struct ican3_msg *msg)
  907. {
  908. netdev_dbg(mod->ndev, "%s: modno %d spec 0x%.2x len %d bytes\n", __func__,
  909. mod->num, msg->spec, le16_to_cpu(msg->len));
  910. switch (msg->spec) {
  911. case MSG_IDVERS:
  912. ican3_handle_idvers(mod, msg);
  913. break;
  914. case MSG_MSGLOST:
  915. case MSG_FMSGLOST:
  916. ican3_handle_msglost(mod, msg);
  917. break;
  918. case MSG_CEVTIND:
  919. ican3_handle_cevtind(mod, msg);
  920. break;
  921. case MSG_INQUIRY:
  922. ican3_handle_inquiry(mod, msg);
  923. break;
  924. default:
  925. ican3_handle_unknown_message(mod, msg);
  926. break;
  927. }
  928. }
  929. /*
  930. * The ican3 needs to store all echo skbs, and therefore cannot
  931. * use the generic infrastructure for this.
  932. */
  933. static void ican3_put_echo_skb(struct ican3_dev *mod, struct sk_buff *skb)
  934. {
  935. skb = can_create_echo_skb(skb);
  936. if (!skb)
  937. return;
  938. /* save this skb for tx interrupt echo handling */
  939. skb_queue_tail(&mod->echoq, skb);
  940. }
  941. static unsigned int ican3_get_echo_skb(struct ican3_dev *mod)
  942. {
  943. struct sk_buff *skb = skb_dequeue(&mod->echoq);
  944. struct can_frame *cf;
  945. u8 dlc;
  946. /* this should never trigger unless there is a driver bug */
  947. if (!skb) {
  948. netdev_err(mod->ndev, "BUG: echo skb not occupied\n");
  949. return 0;
  950. }
  951. cf = (struct can_frame *)skb->data;
  952. dlc = cf->can_dlc;
  953. /* check flag whether this packet has to be looped back */
  954. if (skb->pkt_type != PACKET_LOOPBACK) {
  955. kfree_skb(skb);
  956. return dlc;
  957. }
  958. skb->protocol = htons(ETH_P_CAN);
  959. skb->pkt_type = PACKET_BROADCAST;
  960. skb->ip_summed = CHECKSUM_UNNECESSARY;
  961. skb->dev = mod->ndev;
  962. netif_receive_skb(skb);
  963. return dlc;
  964. }
  965. /*
  966. * Compare an skb with an existing echo skb
  967. *
  968. * This function will be used on devices which have a hardware loopback.
  969. * On these devices, this function can be used to compare a received skb
  970. * with the saved echo skbs so that the hardware echo skb can be dropped.
  971. *
  972. * Returns true if the skb's are identical, false otherwise.
  973. */
  974. static bool ican3_echo_skb_matches(struct ican3_dev *mod, struct sk_buff *skb)
  975. {
  976. struct can_frame *cf = (struct can_frame *)skb->data;
  977. struct sk_buff *echo_skb = skb_peek(&mod->echoq);
  978. struct can_frame *echo_cf;
  979. if (!echo_skb)
  980. return false;
  981. echo_cf = (struct can_frame *)echo_skb->data;
  982. if (cf->can_id != echo_cf->can_id)
  983. return false;
  984. if (cf->can_dlc != echo_cf->can_dlc)
  985. return false;
  986. return memcmp(cf->data, echo_cf->data, cf->can_dlc) == 0;
  987. }
  988. /*
  989. * Check that there is room in the TX ring to transmit another skb
  990. *
  991. * LOCKING: must hold mod->lock
  992. */
  993. static bool ican3_txok(struct ican3_dev *mod)
  994. {
  995. struct ican3_fast_desc __iomem *desc;
  996. u8 control;
  997. /* check that we have echo queue space */
  998. if (skb_queue_len(&mod->echoq) >= ICAN3_TX_BUFFERS)
  999. return false;
  1000. /* copy the control bits of the descriptor */
  1001. ican3_set_page(mod, mod->fasttx_start + (mod->fasttx_num / 16));
  1002. desc = mod->dpm + ((mod->fasttx_num % 16) * sizeof(*desc));
  1003. control = ioread8(&desc->control);
  1004. /* if the control bits are not valid, then we have no more space */
  1005. if (!(control & DESC_VALID))
  1006. return false;
  1007. return true;
  1008. }
  1009. /*
  1010. * Receive one CAN frame from the hardware
  1011. *
  1012. * CONTEXT: must be called from user context
  1013. */
  1014. static int ican3_recv_skb(struct ican3_dev *mod)
  1015. {
  1016. struct net_device *ndev = mod->ndev;
  1017. struct net_device_stats *stats = &ndev->stats;
  1018. struct ican3_fast_desc desc;
  1019. void __iomem *desc_addr;
  1020. struct can_frame *cf;
  1021. struct sk_buff *skb;
  1022. unsigned long flags;
  1023. spin_lock_irqsave(&mod->lock, flags);
  1024. /* copy the whole descriptor */
  1025. ican3_set_page(mod, mod->fastrx_start + (mod->fastrx_num / 16));
  1026. desc_addr = mod->dpm + ((mod->fastrx_num % 16) * sizeof(desc));
  1027. memcpy_fromio(&desc, desc_addr, sizeof(desc));
  1028. spin_unlock_irqrestore(&mod->lock, flags);
  1029. /* check that we actually have a CAN frame */
  1030. if (!(desc.control & DESC_VALID))
  1031. return -ENOBUFS;
  1032. /* allocate an skb */
  1033. skb = alloc_can_skb(ndev, &cf);
  1034. if (unlikely(skb == NULL)) {
  1035. stats->rx_dropped++;
  1036. goto err_noalloc;
  1037. }
  1038. /* convert the ICAN3 frame into Linux CAN format */
  1039. ican3_to_can_frame(mod, &desc, cf);
  1040. /*
  1041. * If this is an ECHO frame received from the hardware loopback
  1042. * feature, use the skb saved in the ECHO stack instead. This allows
  1043. * the Linux CAN core to support CAN_RAW_RECV_OWN_MSGS correctly.
  1044. *
  1045. * Since this is a confirmation of a successfully transmitted packet
  1046. * sent from this host, update the transmit statistics.
  1047. *
  1048. * Also, the netdevice queue needs to be allowed to send packets again.
  1049. */
  1050. if (ican3_echo_skb_matches(mod, skb)) {
  1051. stats->tx_packets++;
  1052. stats->tx_bytes += ican3_get_echo_skb(mod);
  1053. kfree_skb(skb);
  1054. goto err_noalloc;
  1055. }
  1056. /* update statistics, receive the skb */
  1057. stats->rx_packets++;
  1058. stats->rx_bytes += cf->can_dlc;
  1059. netif_receive_skb(skb);
  1060. err_noalloc:
  1061. /* toggle the valid bit and return the descriptor to the ring */
  1062. desc.control ^= DESC_VALID;
  1063. spin_lock_irqsave(&mod->lock, flags);
  1064. ican3_set_page(mod, mod->fastrx_start + (mod->fastrx_num / 16));
  1065. memcpy_toio(desc_addr, &desc, 1);
  1066. /* update the next buffer pointer */
  1067. mod->fastrx_num = (desc.control & DESC_WRAP) ? 0
  1068. : (mod->fastrx_num + 1);
  1069. /* there are still more buffers to process */
  1070. spin_unlock_irqrestore(&mod->lock, flags);
  1071. return 0;
  1072. }
  1073. static int ican3_napi(struct napi_struct *napi, int budget)
  1074. {
  1075. struct ican3_dev *mod = container_of(napi, struct ican3_dev, napi);
  1076. unsigned long flags;
  1077. int received = 0;
  1078. int ret;
  1079. /* process all communication messages */
  1080. while (true) {
  1081. struct ican3_msg uninitialized_var(msg);
  1082. ret = ican3_recv_msg(mod, &msg);
  1083. if (ret)
  1084. break;
  1085. ican3_handle_message(mod, &msg);
  1086. }
  1087. /* process all CAN frames from the fast interface */
  1088. while (received < budget) {
  1089. ret = ican3_recv_skb(mod);
  1090. if (ret)
  1091. break;
  1092. received++;
  1093. }
  1094. /* We have processed all packets that the adapter had, but it
  1095. * was less than our budget, stop polling */
  1096. if (received < budget)
  1097. napi_complete(napi);
  1098. spin_lock_irqsave(&mod->lock, flags);
  1099. /* Wake up the transmit queue if necessary */
  1100. if (netif_queue_stopped(mod->ndev) && ican3_txok(mod))
  1101. netif_wake_queue(mod->ndev);
  1102. spin_unlock_irqrestore(&mod->lock, flags);
  1103. /* re-enable interrupt generation */
  1104. iowrite8(1 << mod->num, &mod->ctrl->int_enable);
  1105. return received;
  1106. }
  1107. static irqreturn_t ican3_irq(int irq, void *dev_id)
  1108. {
  1109. struct ican3_dev *mod = dev_id;
  1110. u8 stat;
  1111. /*
  1112. * The interrupt status register on this device reports interrupts
  1113. * as zeroes instead of using ones like most other devices
  1114. */
  1115. stat = ioread8(&mod->ctrl->int_disable) & (1 << mod->num);
  1116. if (stat == (1 << mod->num))
  1117. return IRQ_NONE;
  1118. /* clear the MODULbus interrupt from the microcontroller */
  1119. ioread8(&mod->dpmctrl->interrupt);
  1120. /* disable interrupt generation, schedule the NAPI poller */
  1121. iowrite8(1 << mod->num, &mod->ctrl->int_disable);
  1122. napi_schedule(&mod->napi);
  1123. return IRQ_HANDLED;
  1124. }
  1125. /*
  1126. * Firmware reset, startup, and shutdown
  1127. */
  1128. /*
  1129. * Reset an ICAN module to its power-on state
  1130. *
  1131. * CONTEXT: no network device registered
  1132. */
  1133. static int ican3_reset_module(struct ican3_dev *mod)
  1134. {
  1135. unsigned long start;
  1136. u8 runold, runnew;
  1137. /* disable interrupts so no more work is scheduled */
  1138. iowrite8(1 << mod->num, &mod->ctrl->int_disable);
  1139. /* the first unallocated page in the DPM is #9 */
  1140. mod->free_page = DPM_FREE_START;
  1141. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  1142. runold = ioread8(mod->dpm + TARGET_RUNNING);
  1143. /* reset the module */
  1144. iowrite8(0x00, &mod->dpmctrl->hwreset);
  1145. /* wait until the module has finished resetting and is running */
  1146. start = jiffies;
  1147. do {
  1148. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  1149. runnew = ioread8(mod->dpm + TARGET_RUNNING);
  1150. if (runnew == (runold ^ 0xff))
  1151. return 0;
  1152. msleep(10);
  1153. } while (time_before(jiffies, start + HZ / 4));
  1154. netdev_err(mod->ndev, "failed to reset CAN module\n");
  1155. return -ETIMEDOUT;
  1156. }
  1157. static void ican3_shutdown_module(struct ican3_dev *mod)
  1158. {
  1159. ican3_msg_disconnect(mod);
  1160. ican3_reset_module(mod);
  1161. }
  1162. /*
  1163. * Startup an ICAN module, bringing it into fast mode
  1164. */
  1165. static int ican3_startup_module(struct ican3_dev *mod)
  1166. {
  1167. int ret;
  1168. ret = ican3_reset_module(mod);
  1169. if (ret) {
  1170. netdev_err(mod->ndev, "unable to reset module\n");
  1171. return ret;
  1172. }
  1173. /* re-enable interrupts so we can send messages */
  1174. iowrite8(1 << mod->num, &mod->ctrl->int_enable);
  1175. ret = ican3_msg_connect(mod);
  1176. if (ret) {
  1177. netdev_err(mod->ndev, "unable to connect to module\n");
  1178. return ret;
  1179. }
  1180. ican3_init_new_host_interface(mod);
  1181. ret = ican3_msg_newhostif(mod);
  1182. if (ret) {
  1183. netdev_err(mod->ndev, "unable to switch to new-style interface\n");
  1184. return ret;
  1185. }
  1186. /* default to "termination on" */
  1187. ret = ican3_set_termination(mod, true);
  1188. if (ret) {
  1189. netdev_err(mod->ndev, "unable to enable termination\n");
  1190. return ret;
  1191. }
  1192. /* default to "bus errors enabled" */
  1193. ret = ican3_set_buserror(mod, 1);
  1194. if (ret) {
  1195. netdev_err(mod->ndev, "unable to set bus-error\n");
  1196. return ret;
  1197. }
  1198. ican3_init_fast_host_interface(mod);
  1199. ret = ican3_msg_fasthostif(mod);
  1200. if (ret) {
  1201. netdev_err(mod->ndev, "unable to switch to fast host interface\n");
  1202. return ret;
  1203. }
  1204. ret = ican3_set_id_filter(mod, true);
  1205. if (ret) {
  1206. netdev_err(mod->ndev, "unable to set acceptance filter\n");
  1207. return ret;
  1208. }
  1209. return 0;
  1210. }
  1211. /*
  1212. * CAN Network Device
  1213. */
  1214. static int ican3_open(struct net_device *ndev)
  1215. {
  1216. struct ican3_dev *mod = netdev_priv(ndev);
  1217. int ret;
  1218. /* open the CAN layer */
  1219. ret = open_candev(ndev);
  1220. if (ret) {
  1221. netdev_err(mod->ndev, "unable to start CAN layer\n");
  1222. return ret;
  1223. }
  1224. /* bring the bus online */
  1225. ret = ican3_set_bus_state(mod, true);
  1226. if (ret) {
  1227. netdev_err(mod->ndev, "unable to set bus-on\n");
  1228. close_candev(ndev);
  1229. return ret;
  1230. }
  1231. /* start up the network device */
  1232. mod->can.state = CAN_STATE_ERROR_ACTIVE;
  1233. netif_start_queue(ndev);
  1234. return 0;
  1235. }
  1236. static int ican3_stop(struct net_device *ndev)
  1237. {
  1238. struct ican3_dev *mod = netdev_priv(ndev);
  1239. int ret;
  1240. /* stop the network device xmit routine */
  1241. netif_stop_queue(ndev);
  1242. mod->can.state = CAN_STATE_STOPPED;
  1243. /* bring the bus offline, stop receiving packets */
  1244. ret = ican3_set_bus_state(mod, false);
  1245. if (ret) {
  1246. netdev_err(mod->ndev, "unable to set bus-off\n");
  1247. return ret;
  1248. }
  1249. /* drop all outstanding echo skbs */
  1250. skb_queue_purge(&mod->echoq);
  1251. /* close the CAN layer */
  1252. close_candev(ndev);
  1253. return 0;
  1254. }
  1255. static int ican3_xmit(struct sk_buff *skb, struct net_device *ndev)
  1256. {
  1257. struct ican3_dev *mod = netdev_priv(ndev);
  1258. struct can_frame *cf = (struct can_frame *)skb->data;
  1259. struct ican3_fast_desc desc;
  1260. void __iomem *desc_addr;
  1261. unsigned long flags;
  1262. if (can_dropped_invalid_skb(ndev, skb))
  1263. return NETDEV_TX_OK;
  1264. spin_lock_irqsave(&mod->lock, flags);
  1265. /* check that we can actually transmit */
  1266. if (!ican3_txok(mod)) {
  1267. netdev_err(mod->ndev, "BUG: no free descriptors\n");
  1268. spin_unlock_irqrestore(&mod->lock, flags);
  1269. return NETDEV_TX_BUSY;
  1270. }
  1271. /* copy the control bits of the descriptor */
  1272. ican3_set_page(mod, mod->fasttx_start + (mod->fasttx_num / 16));
  1273. desc_addr = mod->dpm + ((mod->fasttx_num % 16) * sizeof(desc));
  1274. memset(&desc, 0, sizeof(desc));
  1275. memcpy_fromio(&desc, desc_addr, 1);
  1276. /* convert the Linux CAN frame into ICAN3 format */
  1277. can_frame_to_ican3(mod, cf, &desc);
  1278. /*
  1279. * This hardware doesn't have TX-done notifications, so we'll try and
  1280. * emulate it the best we can using ECHO skbs. Add the skb to the ECHO
  1281. * stack. Upon packet reception, check if the ECHO skb and received
  1282. * skb match, and use that to wake the queue.
  1283. */
  1284. ican3_put_echo_skb(mod, skb);
  1285. /*
  1286. * the programming manual says that you must set the IVALID bit, then
  1287. * interrupt, then set the valid bit. Quite weird, but it seems to be
  1288. * required for this to work
  1289. */
  1290. desc.control |= DESC_IVALID;
  1291. memcpy_toio(desc_addr, &desc, sizeof(desc));
  1292. /* generate a MODULbus interrupt to the microcontroller */
  1293. iowrite8(0x01, &mod->dpmctrl->interrupt);
  1294. desc.control ^= DESC_VALID;
  1295. memcpy_toio(desc_addr, &desc, sizeof(desc));
  1296. /* update the next buffer pointer */
  1297. mod->fasttx_num = (desc.control & DESC_WRAP) ? 0
  1298. : (mod->fasttx_num + 1);
  1299. /* if there is no free descriptor space, stop the transmit queue */
  1300. if (!ican3_txok(mod))
  1301. netif_stop_queue(ndev);
  1302. spin_unlock_irqrestore(&mod->lock, flags);
  1303. return NETDEV_TX_OK;
  1304. }
  1305. static const struct net_device_ops ican3_netdev_ops = {
  1306. .ndo_open = ican3_open,
  1307. .ndo_stop = ican3_stop,
  1308. .ndo_start_xmit = ican3_xmit,
  1309. .ndo_change_mtu = can_change_mtu,
  1310. };
  1311. /*
  1312. * Low-level CAN Device
  1313. */
  1314. /* This structure was stolen from drivers/net/can/sja1000/sja1000.c */
  1315. static const struct can_bittiming_const ican3_bittiming_const = {
  1316. .name = DRV_NAME,
  1317. .tseg1_min = 1,
  1318. .tseg1_max = 16,
  1319. .tseg2_min = 1,
  1320. .tseg2_max = 8,
  1321. .sjw_max = 4,
  1322. .brp_min = 1,
  1323. .brp_max = 64,
  1324. .brp_inc = 1,
  1325. };
  1326. /*
  1327. * This routine was stolen from drivers/net/can/sja1000/sja1000.c
  1328. *
  1329. * The bittiming register command for the ICAN3 just sets the bit timing
  1330. * registers on the SJA1000 chip directly
  1331. */
  1332. static int ican3_set_bittiming(struct net_device *ndev)
  1333. {
  1334. struct ican3_dev *mod = netdev_priv(ndev);
  1335. struct can_bittiming *bt = &mod->can.bittiming;
  1336. struct ican3_msg msg;
  1337. u8 btr0, btr1;
  1338. btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
  1339. btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
  1340. (((bt->phase_seg2 - 1) & 0x7) << 4);
  1341. if (mod->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  1342. btr1 |= 0x80;
  1343. memset(&msg, 0, sizeof(msg));
  1344. msg.spec = MSG_CBTRREQ;
  1345. msg.len = cpu_to_le16(4);
  1346. msg.data[0] = 0x00;
  1347. msg.data[1] = 0x00;
  1348. msg.data[2] = btr0;
  1349. msg.data[3] = btr1;
  1350. return ican3_send_msg(mod, &msg);
  1351. }
  1352. static int ican3_set_mode(struct net_device *ndev, enum can_mode mode)
  1353. {
  1354. struct ican3_dev *mod = netdev_priv(ndev);
  1355. int ret;
  1356. if (mode != CAN_MODE_START)
  1357. return -ENOTSUPP;
  1358. /* bring the bus online */
  1359. ret = ican3_set_bus_state(mod, true);
  1360. if (ret) {
  1361. netdev_err(ndev, "unable to set bus-on\n");
  1362. return ret;
  1363. }
  1364. /* start up the network device */
  1365. mod->can.state = CAN_STATE_ERROR_ACTIVE;
  1366. if (netif_queue_stopped(ndev))
  1367. netif_wake_queue(ndev);
  1368. return 0;
  1369. }
  1370. static int ican3_get_berr_counter(const struct net_device *ndev,
  1371. struct can_berr_counter *bec)
  1372. {
  1373. struct ican3_dev *mod = netdev_priv(ndev);
  1374. int ret;
  1375. ret = ican3_send_inquiry(mod, INQUIRY_STATUS);
  1376. if (ret)
  1377. return ret;
  1378. if (!wait_for_completion_timeout(&mod->buserror_comp, HZ)) {
  1379. netdev_info(mod->ndev, "%s timed out\n", __func__);
  1380. return -ETIMEDOUT;
  1381. }
  1382. bec->rxerr = mod->bec.rxerr;
  1383. bec->txerr = mod->bec.txerr;
  1384. return 0;
  1385. }
  1386. /*
  1387. * Sysfs Attributes
  1388. */
  1389. static ssize_t ican3_sysfs_show_term(struct device *dev,
  1390. struct device_attribute *attr,
  1391. char *buf)
  1392. {
  1393. struct ican3_dev *mod = netdev_priv(to_net_dev(dev));
  1394. int ret;
  1395. ret = ican3_send_inquiry(mod, INQUIRY_TERMINATION);
  1396. if (ret)
  1397. return ret;
  1398. if (!wait_for_completion_timeout(&mod->termination_comp, HZ)) {
  1399. netdev_info(mod->ndev, "%s timed out\n", __func__);
  1400. return -ETIMEDOUT;
  1401. }
  1402. return snprintf(buf, PAGE_SIZE, "%u\n", mod->termination_enabled);
  1403. }
  1404. static ssize_t ican3_sysfs_set_term(struct device *dev,
  1405. struct device_attribute *attr,
  1406. const char *buf, size_t count)
  1407. {
  1408. struct ican3_dev *mod = netdev_priv(to_net_dev(dev));
  1409. unsigned long enable;
  1410. int ret;
  1411. if (kstrtoul(buf, 0, &enable))
  1412. return -EINVAL;
  1413. ret = ican3_set_termination(mod, enable);
  1414. if (ret)
  1415. return ret;
  1416. return count;
  1417. }
  1418. static DEVICE_ATTR(termination, S_IWUSR | S_IRUGO, ican3_sysfs_show_term,
  1419. ican3_sysfs_set_term);
  1420. static struct attribute *ican3_sysfs_attrs[] = {
  1421. &dev_attr_termination.attr,
  1422. NULL,
  1423. };
  1424. static struct attribute_group ican3_sysfs_attr_group = {
  1425. .attrs = ican3_sysfs_attrs,
  1426. };
  1427. /*
  1428. * PCI Subsystem
  1429. */
  1430. static int ican3_probe(struct platform_device *pdev)
  1431. {
  1432. struct janz_platform_data *pdata;
  1433. struct net_device *ndev;
  1434. struct ican3_dev *mod;
  1435. struct resource *res;
  1436. struct device *dev;
  1437. int ret;
  1438. pdata = dev_get_platdata(&pdev->dev);
  1439. if (!pdata)
  1440. return -ENXIO;
  1441. dev_dbg(&pdev->dev, "probe: module number %d\n", pdata->modno);
  1442. /* save the struct device for printing */
  1443. dev = &pdev->dev;
  1444. /* allocate the CAN device and private data */
  1445. ndev = alloc_candev(sizeof(*mod), 0);
  1446. if (!ndev) {
  1447. dev_err(dev, "unable to allocate CANdev\n");
  1448. ret = -ENOMEM;
  1449. goto out_return;
  1450. }
  1451. platform_set_drvdata(pdev, ndev);
  1452. mod = netdev_priv(ndev);
  1453. mod->ndev = ndev;
  1454. mod->num = pdata->modno;
  1455. netif_napi_add(ndev, &mod->napi, ican3_napi, ICAN3_RX_BUFFERS);
  1456. skb_queue_head_init(&mod->echoq);
  1457. spin_lock_init(&mod->lock);
  1458. init_completion(&mod->termination_comp);
  1459. init_completion(&mod->buserror_comp);
  1460. /* setup device-specific sysfs attributes */
  1461. ndev->sysfs_groups[0] = &ican3_sysfs_attr_group;
  1462. /* the first unallocated page in the DPM is 9 */
  1463. mod->free_page = DPM_FREE_START;
  1464. ndev->netdev_ops = &ican3_netdev_ops;
  1465. ndev->flags |= IFF_ECHO;
  1466. SET_NETDEV_DEV(ndev, &pdev->dev);
  1467. mod->can.clock.freq = ICAN3_CAN_CLOCK;
  1468. mod->can.bittiming_const = &ican3_bittiming_const;
  1469. mod->can.do_set_bittiming = ican3_set_bittiming;
  1470. mod->can.do_set_mode = ican3_set_mode;
  1471. mod->can.do_get_berr_counter = ican3_get_berr_counter;
  1472. mod->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES
  1473. | CAN_CTRLMODE_BERR_REPORTING
  1474. | CAN_CTRLMODE_ONE_SHOT;
  1475. /* find our IRQ number */
  1476. mod->irq = platform_get_irq(pdev, 0);
  1477. if (mod->irq < 0) {
  1478. dev_err(dev, "IRQ line not found\n");
  1479. ret = -ENODEV;
  1480. goto out_free_ndev;
  1481. }
  1482. ndev->irq = mod->irq;
  1483. /* get access to the MODULbus registers for this module */
  1484. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1485. if (!res) {
  1486. dev_err(dev, "MODULbus registers not found\n");
  1487. ret = -ENODEV;
  1488. goto out_free_ndev;
  1489. }
  1490. mod->dpm = ioremap(res->start, resource_size(res));
  1491. if (!mod->dpm) {
  1492. dev_err(dev, "MODULbus registers not ioremap\n");
  1493. ret = -ENOMEM;
  1494. goto out_free_ndev;
  1495. }
  1496. mod->dpmctrl = mod->dpm + DPM_PAGE_SIZE;
  1497. /* get access to the control registers for this module */
  1498. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1499. if (!res) {
  1500. dev_err(dev, "CONTROL registers not found\n");
  1501. ret = -ENODEV;
  1502. goto out_iounmap_dpm;
  1503. }
  1504. mod->ctrl = ioremap(res->start, resource_size(res));
  1505. if (!mod->ctrl) {
  1506. dev_err(dev, "CONTROL registers not ioremap\n");
  1507. ret = -ENOMEM;
  1508. goto out_iounmap_dpm;
  1509. }
  1510. /* disable our IRQ, then hookup the IRQ handler */
  1511. iowrite8(1 << mod->num, &mod->ctrl->int_disable);
  1512. ret = request_irq(mod->irq, ican3_irq, IRQF_SHARED, DRV_NAME, mod);
  1513. if (ret) {
  1514. dev_err(dev, "unable to request IRQ\n");
  1515. goto out_iounmap_ctrl;
  1516. }
  1517. /* reset and initialize the CAN controller into fast mode */
  1518. napi_enable(&mod->napi);
  1519. ret = ican3_startup_module(mod);
  1520. if (ret) {
  1521. dev_err(dev, "%s: unable to start CANdev\n", __func__);
  1522. goto out_free_irq;
  1523. }
  1524. /* register with the Linux CAN layer */
  1525. ret = register_candev(ndev);
  1526. if (ret) {
  1527. dev_err(dev, "%s: unable to register CANdev\n", __func__);
  1528. goto out_free_irq;
  1529. }
  1530. dev_info(dev, "module %d: registered CAN device\n", pdata->modno);
  1531. return 0;
  1532. out_free_irq:
  1533. napi_disable(&mod->napi);
  1534. iowrite8(1 << mod->num, &mod->ctrl->int_disable);
  1535. free_irq(mod->irq, mod);
  1536. out_iounmap_ctrl:
  1537. iounmap(mod->ctrl);
  1538. out_iounmap_dpm:
  1539. iounmap(mod->dpm);
  1540. out_free_ndev:
  1541. free_candev(ndev);
  1542. out_return:
  1543. return ret;
  1544. }
  1545. static int ican3_remove(struct platform_device *pdev)
  1546. {
  1547. struct net_device *ndev = platform_get_drvdata(pdev);
  1548. struct ican3_dev *mod = netdev_priv(ndev);
  1549. /* unregister the netdevice, stop interrupts */
  1550. unregister_netdev(ndev);
  1551. napi_disable(&mod->napi);
  1552. iowrite8(1 << mod->num, &mod->ctrl->int_disable);
  1553. free_irq(mod->irq, mod);
  1554. /* put the module into reset */
  1555. ican3_shutdown_module(mod);
  1556. /* unmap all registers */
  1557. iounmap(mod->ctrl);
  1558. iounmap(mod->dpm);
  1559. free_candev(ndev);
  1560. return 0;
  1561. }
  1562. static struct platform_driver ican3_driver = {
  1563. .driver = {
  1564. .name = DRV_NAME,
  1565. },
  1566. .probe = ican3_probe,
  1567. .remove = ican3_remove,
  1568. };
  1569. module_platform_driver(ican3_driver);
  1570. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  1571. MODULE_DESCRIPTION("Janz MODULbus VMOD-ICAN3 Driver");
  1572. MODULE_LICENSE("GPL");
  1573. MODULE_ALIAS("platform:janz-ican3");