janz-ican3.c 48 KB

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