janz-ican3.c 44 KB

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