bfin_can.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * Blackfin On-Chip CAN Driver
  3. *
  4. * Copyright 2004-2009 Analog Devices Inc.
  5. *
  6. * Enter bugs at http://blackfin.uclinux.org/
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/bitops.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/errno.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/can/dev.h>
  19. #include <linux/can/error.h>
  20. #include <asm/portmux.h>
  21. #define DRV_NAME "bfin_can"
  22. #define BFIN_CAN_TIMEOUT 100
  23. #define TX_ECHO_SKB_MAX 1
  24. /* transmit and receive channels */
  25. #define TRANSMIT_CHL 24
  26. #define RECEIVE_STD_CHL 0
  27. #define RECEIVE_EXT_CHL 4
  28. #define RECEIVE_RTR_CHL 8
  29. #define RECEIVE_EXT_RTR_CHL 12
  30. #define MAX_CHL_NUMBER 32
  31. /* All Blackfin system MMRs are padded to 32bits even if the register
  32. * itself is only 16bits. So use a helper macro to streamline this
  33. */
  34. #define __BFP(m) u16 m; u16 __pad_##m
  35. /* bfin can registers layout */
  36. struct bfin_can_mask_regs {
  37. __BFP(aml);
  38. __BFP(amh);
  39. };
  40. struct bfin_can_channel_regs {
  41. /* data[0,2,4,6] -> data{0,1,2,3} while data[1,3,5,7] is padding */
  42. u16 data[8];
  43. __BFP(dlc);
  44. __BFP(tsv);
  45. __BFP(id0);
  46. __BFP(id1);
  47. };
  48. struct bfin_can_regs {
  49. /* global control and status registers */
  50. __BFP(mc1); /* offset 0x00 */
  51. __BFP(md1); /* offset 0x04 */
  52. __BFP(trs1); /* offset 0x08 */
  53. __BFP(trr1); /* offset 0x0c */
  54. __BFP(ta1); /* offset 0x10 */
  55. __BFP(aa1); /* offset 0x14 */
  56. __BFP(rmp1); /* offset 0x18 */
  57. __BFP(rml1); /* offset 0x1c */
  58. __BFP(mbtif1); /* offset 0x20 */
  59. __BFP(mbrif1); /* offset 0x24 */
  60. __BFP(mbim1); /* offset 0x28 */
  61. __BFP(rfh1); /* offset 0x2c */
  62. __BFP(opss1); /* offset 0x30 */
  63. u32 __pad1[3];
  64. __BFP(mc2); /* offset 0x40 */
  65. __BFP(md2); /* offset 0x44 */
  66. __BFP(trs2); /* offset 0x48 */
  67. __BFP(trr2); /* offset 0x4c */
  68. __BFP(ta2); /* offset 0x50 */
  69. __BFP(aa2); /* offset 0x54 */
  70. __BFP(rmp2); /* offset 0x58 */
  71. __BFP(rml2); /* offset 0x5c */
  72. __BFP(mbtif2); /* offset 0x60 */
  73. __BFP(mbrif2); /* offset 0x64 */
  74. __BFP(mbim2); /* offset 0x68 */
  75. __BFP(rfh2); /* offset 0x6c */
  76. __BFP(opss2); /* offset 0x70 */
  77. u32 __pad2[3];
  78. __BFP(clock); /* offset 0x80 */
  79. __BFP(timing); /* offset 0x84 */
  80. __BFP(debug); /* offset 0x88 */
  81. __BFP(status); /* offset 0x8c */
  82. __BFP(cec); /* offset 0x90 */
  83. __BFP(gis); /* offset 0x94 */
  84. __BFP(gim); /* offset 0x98 */
  85. __BFP(gif); /* offset 0x9c */
  86. __BFP(control); /* offset 0xa0 */
  87. __BFP(intr); /* offset 0xa4 */
  88. __BFP(version); /* offset 0xa8 */
  89. __BFP(mbtd); /* offset 0xac */
  90. __BFP(ewr); /* offset 0xb0 */
  91. __BFP(esr); /* offset 0xb4 */
  92. u32 __pad3[2];
  93. __BFP(ucreg); /* offset 0xc0 */
  94. __BFP(uccnt); /* offset 0xc4 */
  95. __BFP(ucrc); /* offset 0xc8 */
  96. __BFP(uccnf); /* offset 0xcc */
  97. u32 __pad4[1];
  98. __BFP(version2); /* offset 0xd4 */
  99. u32 __pad5[10];
  100. /* channel(mailbox) mask and message registers */
  101. struct bfin_can_mask_regs msk[MAX_CHL_NUMBER]; /* offset 0x100 */
  102. struct bfin_can_channel_regs chl[MAX_CHL_NUMBER]; /* offset 0x200 */
  103. };
  104. #undef __BFP
  105. #define SRS 0x0001 /* Software Reset */
  106. #define SER 0x0008 /* Stuff Error */
  107. #define BOIM 0x0008 /* Enable Bus Off Interrupt */
  108. #define CCR 0x0080 /* CAN Configuration Mode Request */
  109. #define CCA 0x0080 /* Configuration Mode Acknowledge */
  110. #define SAM 0x0080 /* Sampling */
  111. #define AME 0x8000 /* Acceptance Mask Enable */
  112. #define RMLIM 0x0080 /* Enable RX Message Lost Interrupt */
  113. #define RMLIS 0x0080 /* RX Message Lost IRQ Status */
  114. #define RTR 0x4000 /* Remote Frame Transmission Request */
  115. #define BOIS 0x0008 /* Bus Off IRQ Status */
  116. #define IDE 0x2000 /* Identifier Extension */
  117. #define EPIS 0x0004 /* Error-Passive Mode IRQ Status */
  118. #define EPIM 0x0004 /* Enable Error-Passive Mode Interrupt */
  119. #define EWTIS 0x0001 /* TX Error Count IRQ Status */
  120. #define EWRIS 0x0002 /* RX Error Count IRQ Status */
  121. #define BEF 0x0040 /* Bit Error Flag */
  122. #define FER 0x0080 /* Form Error Flag */
  123. #define SMR 0x0020 /* Sleep Mode Request */
  124. #define SMACK 0x0008 /* Sleep Mode Acknowledge */
  125. /*
  126. * bfin can private data
  127. */
  128. struct bfin_can_priv {
  129. struct can_priv can; /* must be the first member */
  130. struct net_device *dev;
  131. void __iomem *membase;
  132. int rx_irq;
  133. int tx_irq;
  134. int err_irq;
  135. unsigned short *pin_list;
  136. };
  137. /*
  138. * bfin can timing parameters
  139. */
  140. static const struct can_bittiming_const bfin_can_bittiming_const = {
  141. .name = DRV_NAME,
  142. .tseg1_min = 1,
  143. .tseg1_max = 16,
  144. .tseg2_min = 1,
  145. .tseg2_max = 8,
  146. .sjw_max = 4,
  147. /*
  148. * Although the BRP field can be set to any value, it is recommended
  149. * that the value be greater than or equal to 4, as restrictions
  150. * apply to the bit timing configuration when BRP is less than 4.
  151. */
  152. .brp_min = 4,
  153. .brp_max = 1024,
  154. .brp_inc = 1,
  155. };
  156. static int bfin_can_set_bittiming(struct net_device *dev)
  157. {
  158. struct bfin_can_priv *priv = netdev_priv(dev);
  159. struct bfin_can_regs __iomem *reg = priv->membase;
  160. struct can_bittiming *bt = &priv->can.bittiming;
  161. u16 clk, timing;
  162. clk = bt->brp - 1;
  163. timing = ((bt->sjw - 1) << 8) | (bt->prop_seg + bt->phase_seg1 - 1) |
  164. ((bt->phase_seg2 - 1) << 4);
  165. /*
  166. * If the SAM bit is set, the input signal is oversampled three times
  167. * at the SCLK rate.
  168. */
  169. if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  170. timing |= SAM;
  171. writew(clk, &reg->clock);
  172. writew(timing, &reg->timing);
  173. netdev_info(dev, "setting CLOCK=0x%04x TIMING=0x%04x\n", clk, timing);
  174. return 0;
  175. }
  176. static void bfin_can_set_reset_mode(struct net_device *dev)
  177. {
  178. struct bfin_can_priv *priv = netdev_priv(dev);
  179. struct bfin_can_regs __iomem *reg = priv->membase;
  180. int timeout = BFIN_CAN_TIMEOUT;
  181. int i;
  182. /* disable interrupts */
  183. writew(0, &reg->mbim1);
  184. writew(0, &reg->mbim2);
  185. writew(0, &reg->gim);
  186. /* reset can and enter configuration mode */
  187. writew(SRS | CCR, &reg->control);
  188. writew(CCR, &reg->control);
  189. while (!(readw(&reg->control) & CCA)) {
  190. udelay(10);
  191. if (--timeout == 0) {
  192. netdev_err(dev, "fail to enter configuration mode\n");
  193. BUG();
  194. }
  195. }
  196. /*
  197. * All mailbox configurations are marked as inactive
  198. * by writing to CAN Mailbox Configuration Registers 1 and 2
  199. * For all bits: 0 - Mailbox disabled, 1 - Mailbox enabled
  200. */
  201. writew(0, &reg->mc1);
  202. writew(0, &reg->mc2);
  203. /* Set Mailbox Direction */
  204. writew(0xFFFF, &reg->md1); /* mailbox 1-16 are RX */
  205. writew(0, &reg->md2); /* mailbox 17-32 are TX */
  206. /* RECEIVE_STD_CHL */
  207. for (i = 0; i < 2; i++) {
  208. writew(0, &reg->chl[RECEIVE_STD_CHL + i].id0);
  209. writew(AME, &reg->chl[RECEIVE_STD_CHL + i].id1);
  210. writew(0, &reg->chl[RECEIVE_STD_CHL + i].dlc);
  211. writew(0x1FFF, &reg->msk[RECEIVE_STD_CHL + i].amh);
  212. writew(0xFFFF, &reg->msk[RECEIVE_STD_CHL + i].aml);
  213. }
  214. /* RECEIVE_EXT_CHL */
  215. for (i = 0; i < 2; i++) {
  216. writew(0, &reg->chl[RECEIVE_EXT_CHL + i].id0);
  217. writew(AME | IDE, &reg->chl[RECEIVE_EXT_CHL + i].id1);
  218. writew(0, &reg->chl[RECEIVE_EXT_CHL + i].dlc);
  219. writew(0x1FFF, &reg->msk[RECEIVE_EXT_CHL + i].amh);
  220. writew(0xFFFF, &reg->msk[RECEIVE_EXT_CHL + i].aml);
  221. }
  222. writew(BIT(TRANSMIT_CHL - 16), &reg->mc2);
  223. writew(BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL), &reg->mc1);
  224. priv->can.state = CAN_STATE_STOPPED;
  225. }
  226. static void bfin_can_set_normal_mode(struct net_device *dev)
  227. {
  228. struct bfin_can_priv *priv = netdev_priv(dev);
  229. struct bfin_can_regs __iomem *reg = priv->membase;
  230. int timeout = BFIN_CAN_TIMEOUT;
  231. /*
  232. * leave configuration mode
  233. */
  234. writew(readw(&reg->control) & ~CCR, &reg->control);
  235. while (readw(&reg->status) & CCA) {
  236. udelay(10);
  237. if (--timeout == 0) {
  238. netdev_err(dev, "fail to leave configuration mode\n");
  239. BUG();
  240. }
  241. }
  242. /*
  243. * clear _All_ tx and rx interrupts
  244. */
  245. writew(0xFFFF, &reg->mbtif1);
  246. writew(0xFFFF, &reg->mbtif2);
  247. writew(0xFFFF, &reg->mbrif1);
  248. writew(0xFFFF, &reg->mbrif2);
  249. /*
  250. * clear global interrupt status register
  251. */
  252. writew(0x7FF, &reg->gis); /* overwrites with '1' */
  253. /*
  254. * Initialize Interrupts
  255. * - set bits in the mailbox interrupt mask register
  256. * - global interrupt mask
  257. */
  258. writew(BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL), &reg->mbim1);
  259. writew(BIT(TRANSMIT_CHL - 16), &reg->mbim2);
  260. writew(EPIM | BOIM | RMLIM, &reg->gim);
  261. }
  262. static void bfin_can_start(struct net_device *dev)
  263. {
  264. struct bfin_can_priv *priv = netdev_priv(dev);
  265. /* enter reset mode */
  266. if (priv->can.state != CAN_STATE_STOPPED)
  267. bfin_can_set_reset_mode(dev);
  268. /* leave reset mode */
  269. bfin_can_set_normal_mode(dev);
  270. }
  271. static int bfin_can_set_mode(struct net_device *dev, enum can_mode mode)
  272. {
  273. switch (mode) {
  274. case CAN_MODE_START:
  275. bfin_can_start(dev);
  276. if (netif_queue_stopped(dev))
  277. netif_wake_queue(dev);
  278. break;
  279. default:
  280. return -EOPNOTSUPP;
  281. }
  282. return 0;
  283. }
  284. static int bfin_can_get_berr_counter(const struct net_device *dev,
  285. struct can_berr_counter *bec)
  286. {
  287. struct bfin_can_priv *priv = netdev_priv(dev);
  288. struct bfin_can_regs __iomem *reg = priv->membase;
  289. u16 cec = readw(&reg->cec);
  290. bec->txerr = cec >> 8;
  291. bec->rxerr = cec;
  292. return 0;
  293. }
  294. static int bfin_can_start_xmit(struct sk_buff *skb, struct net_device *dev)
  295. {
  296. struct bfin_can_priv *priv = netdev_priv(dev);
  297. struct bfin_can_regs __iomem *reg = priv->membase;
  298. struct can_frame *cf = (struct can_frame *)skb->data;
  299. u8 dlc = cf->can_dlc;
  300. canid_t id = cf->can_id;
  301. u8 *data = cf->data;
  302. u16 val;
  303. int i;
  304. if (can_dropped_invalid_skb(dev, skb))
  305. return NETDEV_TX_OK;
  306. netif_stop_queue(dev);
  307. /* fill id */
  308. if (id & CAN_EFF_FLAG) {
  309. writew(id, &reg->chl[TRANSMIT_CHL].id0);
  310. val = ((id & 0x1FFF0000) >> 16) | IDE;
  311. } else
  312. val = (id << 2);
  313. if (id & CAN_RTR_FLAG)
  314. val |= RTR;
  315. writew(val | AME, &reg->chl[TRANSMIT_CHL].id1);
  316. /* fill payload */
  317. for (i = 0; i < 8; i += 2) {
  318. val = ((7 - i) < dlc ? (data[7 - i]) : 0) +
  319. ((6 - i) < dlc ? (data[6 - i] << 8) : 0);
  320. writew(val, &reg->chl[TRANSMIT_CHL].data[i]);
  321. }
  322. /* fill data length code */
  323. writew(dlc, &reg->chl[TRANSMIT_CHL].dlc);
  324. can_put_echo_skb(skb, dev, 0);
  325. /* set transmit request */
  326. writew(BIT(TRANSMIT_CHL - 16), &reg->trs2);
  327. return 0;
  328. }
  329. static void bfin_can_rx(struct net_device *dev, u16 isrc)
  330. {
  331. struct bfin_can_priv *priv = netdev_priv(dev);
  332. struct net_device_stats *stats = &dev->stats;
  333. struct bfin_can_regs __iomem *reg = priv->membase;
  334. struct can_frame *cf;
  335. struct sk_buff *skb;
  336. int obj;
  337. int i;
  338. u16 val;
  339. skb = alloc_can_skb(dev, &cf);
  340. if (skb == NULL)
  341. return;
  342. /* get id */
  343. if (isrc & BIT(RECEIVE_EXT_CHL)) {
  344. /* extended frame format (EFF) */
  345. cf->can_id = ((readw(&reg->chl[RECEIVE_EXT_CHL].id1)
  346. & 0x1FFF) << 16)
  347. + readw(&reg->chl[RECEIVE_EXT_CHL].id0);
  348. cf->can_id |= CAN_EFF_FLAG;
  349. obj = RECEIVE_EXT_CHL;
  350. } else {
  351. /* standard frame format (SFF) */
  352. cf->can_id = (readw(&reg->chl[RECEIVE_STD_CHL].id1)
  353. & 0x1ffc) >> 2;
  354. obj = RECEIVE_STD_CHL;
  355. }
  356. if (readw(&reg->chl[obj].id1) & RTR)
  357. cf->can_id |= CAN_RTR_FLAG;
  358. /* get data length code */
  359. cf->can_dlc = get_can_dlc(readw(&reg->chl[obj].dlc) & 0xF);
  360. /* get payload */
  361. for (i = 0; i < 8; i += 2) {
  362. val = readw(&reg->chl[obj].data[i]);
  363. cf->data[7 - i] = (7 - i) < cf->can_dlc ? val : 0;
  364. cf->data[6 - i] = (6 - i) < cf->can_dlc ? (val >> 8) : 0;
  365. }
  366. stats->rx_packets++;
  367. stats->rx_bytes += cf->can_dlc;
  368. netif_rx(skb);
  369. }
  370. static int bfin_can_err(struct net_device *dev, u16 isrc, u16 status)
  371. {
  372. struct bfin_can_priv *priv = netdev_priv(dev);
  373. struct bfin_can_regs __iomem *reg = priv->membase;
  374. struct net_device_stats *stats = &dev->stats;
  375. struct can_frame *cf;
  376. struct sk_buff *skb;
  377. enum can_state state = priv->can.state;
  378. skb = alloc_can_err_skb(dev, &cf);
  379. if (skb == NULL)
  380. return -ENOMEM;
  381. if (isrc & RMLIS) {
  382. /* data overrun interrupt */
  383. netdev_dbg(dev, "data overrun interrupt\n");
  384. cf->can_id |= CAN_ERR_CRTL;
  385. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  386. stats->rx_over_errors++;
  387. stats->rx_errors++;
  388. }
  389. if (isrc & BOIS) {
  390. netdev_dbg(dev, "bus-off mode interrupt\n");
  391. state = CAN_STATE_BUS_OFF;
  392. cf->can_id |= CAN_ERR_BUSOFF;
  393. priv->can.can_stats.bus_off++;
  394. can_bus_off(dev);
  395. }
  396. if (isrc & EPIS) {
  397. /* error passive interrupt */
  398. netdev_dbg(dev, "error passive interrupt\n");
  399. state = CAN_STATE_ERROR_PASSIVE;
  400. }
  401. if ((isrc & EWTIS) || (isrc & EWRIS)) {
  402. netdev_dbg(dev, "Error Warning Transmit/Receive Interrupt\n");
  403. state = CAN_STATE_ERROR_WARNING;
  404. }
  405. if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING ||
  406. state == CAN_STATE_ERROR_PASSIVE)) {
  407. u16 cec = readw(&reg->cec);
  408. u8 rxerr = cec;
  409. u8 txerr = cec >> 8;
  410. cf->can_id |= CAN_ERR_CRTL;
  411. if (state == CAN_STATE_ERROR_WARNING) {
  412. priv->can.can_stats.error_warning++;
  413. cf->data[1] = (txerr > rxerr) ?
  414. CAN_ERR_CRTL_TX_WARNING :
  415. CAN_ERR_CRTL_RX_WARNING;
  416. } else {
  417. priv->can.can_stats.error_passive++;
  418. cf->data[1] = (txerr > rxerr) ?
  419. CAN_ERR_CRTL_TX_PASSIVE :
  420. CAN_ERR_CRTL_RX_PASSIVE;
  421. }
  422. }
  423. if (status) {
  424. priv->can.can_stats.bus_error++;
  425. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  426. if (status & BEF)
  427. cf->data[2] |= CAN_ERR_PROT_BIT;
  428. else if (status & FER)
  429. cf->data[2] |= CAN_ERR_PROT_FORM;
  430. else if (status & SER)
  431. cf->data[2] |= CAN_ERR_PROT_STUFF;
  432. }
  433. priv->can.state = state;
  434. stats->rx_packets++;
  435. stats->rx_bytes += cf->can_dlc;
  436. netif_rx(skb);
  437. return 0;
  438. }
  439. static irqreturn_t bfin_can_interrupt(int irq, void *dev_id)
  440. {
  441. struct net_device *dev = dev_id;
  442. struct bfin_can_priv *priv = netdev_priv(dev);
  443. struct bfin_can_regs __iomem *reg = priv->membase;
  444. struct net_device_stats *stats = &dev->stats;
  445. u16 status, isrc;
  446. if ((irq == priv->tx_irq) && readw(&reg->mbtif2)) {
  447. /* transmission complete interrupt */
  448. writew(0xFFFF, &reg->mbtif2);
  449. stats->tx_packets++;
  450. stats->tx_bytes += readw(&reg->chl[TRANSMIT_CHL].dlc);
  451. can_get_echo_skb(dev, 0);
  452. netif_wake_queue(dev);
  453. } else if ((irq == priv->rx_irq) && readw(&reg->mbrif1)) {
  454. /* receive interrupt */
  455. isrc = readw(&reg->mbrif1);
  456. writew(0xFFFF, &reg->mbrif1);
  457. bfin_can_rx(dev, isrc);
  458. } else if ((irq == priv->err_irq) && readw(&reg->gis)) {
  459. /* error interrupt */
  460. isrc = readw(&reg->gis);
  461. status = readw(&reg->esr);
  462. writew(0x7FF, &reg->gis);
  463. bfin_can_err(dev, isrc, status);
  464. } else {
  465. return IRQ_NONE;
  466. }
  467. return IRQ_HANDLED;
  468. }
  469. static int bfin_can_open(struct net_device *dev)
  470. {
  471. struct bfin_can_priv *priv = netdev_priv(dev);
  472. int err;
  473. /* set chip into reset mode */
  474. bfin_can_set_reset_mode(dev);
  475. /* common open */
  476. err = open_candev(dev);
  477. if (err)
  478. goto exit_open;
  479. /* register interrupt handler */
  480. err = request_irq(priv->rx_irq, &bfin_can_interrupt, 0,
  481. "bfin-can-rx", dev);
  482. if (err)
  483. goto exit_rx_irq;
  484. err = request_irq(priv->tx_irq, &bfin_can_interrupt, 0,
  485. "bfin-can-tx", dev);
  486. if (err)
  487. goto exit_tx_irq;
  488. err = request_irq(priv->err_irq, &bfin_can_interrupt, 0,
  489. "bfin-can-err", dev);
  490. if (err)
  491. goto exit_err_irq;
  492. bfin_can_start(dev);
  493. netif_start_queue(dev);
  494. return 0;
  495. exit_err_irq:
  496. free_irq(priv->tx_irq, dev);
  497. exit_tx_irq:
  498. free_irq(priv->rx_irq, dev);
  499. exit_rx_irq:
  500. close_candev(dev);
  501. exit_open:
  502. return err;
  503. }
  504. static int bfin_can_close(struct net_device *dev)
  505. {
  506. struct bfin_can_priv *priv = netdev_priv(dev);
  507. netif_stop_queue(dev);
  508. bfin_can_set_reset_mode(dev);
  509. close_candev(dev);
  510. free_irq(priv->rx_irq, dev);
  511. free_irq(priv->tx_irq, dev);
  512. free_irq(priv->err_irq, dev);
  513. return 0;
  514. }
  515. static struct net_device *alloc_bfin_candev(void)
  516. {
  517. struct net_device *dev;
  518. struct bfin_can_priv *priv;
  519. dev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
  520. if (!dev)
  521. return NULL;
  522. priv = netdev_priv(dev);
  523. priv->dev = dev;
  524. priv->can.bittiming_const = &bfin_can_bittiming_const;
  525. priv->can.do_set_bittiming = bfin_can_set_bittiming;
  526. priv->can.do_set_mode = bfin_can_set_mode;
  527. priv->can.do_get_berr_counter = bfin_can_get_berr_counter;
  528. priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
  529. return dev;
  530. }
  531. static const struct net_device_ops bfin_can_netdev_ops = {
  532. .ndo_open = bfin_can_open,
  533. .ndo_stop = bfin_can_close,
  534. .ndo_start_xmit = bfin_can_start_xmit,
  535. .ndo_change_mtu = can_change_mtu,
  536. };
  537. static int bfin_can_probe(struct platform_device *pdev)
  538. {
  539. int err;
  540. struct net_device *dev;
  541. struct bfin_can_priv *priv;
  542. struct resource *res_mem, *rx_irq, *tx_irq, *err_irq;
  543. unsigned short *pdata;
  544. pdata = dev_get_platdata(&pdev->dev);
  545. if (!pdata) {
  546. dev_err(&pdev->dev, "No platform data provided!\n");
  547. err = -EINVAL;
  548. goto exit;
  549. }
  550. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  551. rx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  552. tx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  553. err_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
  554. if (!res_mem || !rx_irq || !tx_irq || !err_irq) {
  555. err = -EINVAL;
  556. goto exit;
  557. }
  558. /* request peripheral pins */
  559. err = peripheral_request_list(pdata, dev_name(&pdev->dev));
  560. if (err)
  561. goto exit;
  562. dev = alloc_bfin_candev();
  563. if (!dev) {
  564. err = -ENOMEM;
  565. goto exit_peri_pin_free;
  566. }
  567. priv = netdev_priv(dev);
  568. priv->membase = devm_ioremap_resource(&pdev->dev, res_mem);
  569. if (IS_ERR(priv->membase)) {
  570. err = PTR_ERR(priv->membase);
  571. goto exit_peri_pin_free;
  572. }
  573. priv->rx_irq = rx_irq->start;
  574. priv->tx_irq = tx_irq->start;
  575. priv->err_irq = err_irq->start;
  576. priv->pin_list = pdata;
  577. priv->can.clock.freq = get_sclk();
  578. platform_set_drvdata(pdev, dev);
  579. SET_NETDEV_DEV(dev, &pdev->dev);
  580. dev->flags |= IFF_ECHO; /* we support local echo */
  581. dev->netdev_ops = &bfin_can_netdev_ops;
  582. bfin_can_set_reset_mode(dev);
  583. err = register_candev(dev);
  584. if (err) {
  585. dev_err(&pdev->dev, "registering failed (err=%d)\n", err);
  586. goto exit_candev_free;
  587. }
  588. dev_info(&pdev->dev,
  589. "%s device registered"
  590. "(&reg_base=%p, rx_irq=%d, tx_irq=%d, err_irq=%d, sclk=%d)\n",
  591. DRV_NAME, priv->membase, priv->rx_irq,
  592. priv->tx_irq, priv->err_irq, priv->can.clock.freq);
  593. return 0;
  594. exit_candev_free:
  595. free_candev(dev);
  596. exit_peri_pin_free:
  597. peripheral_free_list(pdata);
  598. exit:
  599. return err;
  600. }
  601. static int bfin_can_remove(struct platform_device *pdev)
  602. {
  603. struct net_device *dev = platform_get_drvdata(pdev);
  604. struct bfin_can_priv *priv = netdev_priv(dev);
  605. bfin_can_set_reset_mode(dev);
  606. unregister_candev(dev);
  607. peripheral_free_list(priv->pin_list);
  608. free_candev(dev);
  609. return 0;
  610. }
  611. #ifdef CONFIG_PM
  612. static int bfin_can_suspend(struct platform_device *pdev, pm_message_t mesg)
  613. {
  614. struct net_device *dev = platform_get_drvdata(pdev);
  615. struct bfin_can_priv *priv = netdev_priv(dev);
  616. struct bfin_can_regs __iomem *reg = priv->membase;
  617. int timeout = BFIN_CAN_TIMEOUT;
  618. if (netif_running(dev)) {
  619. /* enter sleep mode */
  620. writew(readw(&reg->control) | SMR, &reg->control);
  621. while (!(readw(&reg->intr) & SMACK)) {
  622. udelay(10);
  623. if (--timeout == 0) {
  624. netdev_err(dev, "fail to enter sleep mode\n");
  625. BUG();
  626. }
  627. }
  628. }
  629. return 0;
  630. }
  631. static int bfin_can_resume(struct platform_device *pdev)
  632. {
  633. struct net_device *dev = platform_get_drvdata(pdev);
  634. struct bfin_can_priv *priv = netdev_priv(dev);
  635. struct bfin_can_regs __iomem *reg = priv->membase;
  636. if (netif_running(dev)) {
  637. /* leave sleep mode */
  638. writew(0, &reg->intr);
  639. }
  640. return 0;
  641. }
  642. #else
  643. #define bfin_can_suspend NULL
  644. #define bfin_can_resume NULL
  645. #endif /* CONFIG_PM */
  646. static struct platform_driver bfin_can_driver = {
  647. .probe = bfin_can_probe,
  648. .remove = bfin_can_remove,
  649. .suspend = bfin_can_suspend,
  650. .resume = bfin_can_resume,
  651. .driver = {
  652. .name = DRV_NAME,
  653. },
  654. };
  655. module_platform_driver(bfin_can_driver);
  656. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  657. MODULE_LICENSE("GPL");
  658. MODULE_DESCRIPTION("Blackfin on-chip CAN netdevice driver");
  659. MODULE_ALIAS("platform:" DRV_NAME);