janz-ican3.c 47 KB

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