hi311x.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /* CAN bus driver for Holt HI3110 CAN Controller with SPI Interface
  2. *
  3. * Copyright(C) Timesys Corporation 2016
  4. *
  5. * Based on Microchip 251x CAN Controller (mcp251x) Linux kernel driver
  6. * Copyright 2009 Christian Pellegrin EVOL S.r.l.
  7. * Copyright 2007 Raymarine UK, Ltd. All Rights Reserved.
  8. * Copyright 2006 Arcom Control Systems Ltd.
  9. *
  10. * Based on CAN bus driver for the CCAN controller written by
  11. * - Sascha Hauer, Marc Kleine-Budde, Pengutronix
  12. * - Simon Kallweit, intefo AG
  13. * Copyright 2007
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License version 2 as
  17. * published by the Free Software Foundation.
  18. */
  19. #include <linux/can/core.h>
  20. #include <linux/can/dev.h>
  21. #include <linux/can/led.h>
  22. #include <linux/clk.h>
  23. #include <linux/completion.h>
  24. #include <linux/delay.h>
  25. #include <linux/device.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/freezer.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/io.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/of.h>
  34. #include <linux/of_device.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/regulator/consumer.h>
  37. #include <linux/slab.h>
  38. #include <linux/spi/spi.h>
  39. #include <linux/uaccess.h>
  40. #define HI3110_MASTER_RESET 0x56
  41. #define HI3110_READ_CTRL0 0xD2
  42. #define HI3110_READ_CTRL1 0xD4
  43. #define HI3110_READ_STATF 0xE2
  44. #define HI3110_WRITE_CTRL0 0x14
  45. #define HI3110_WRITE_CTRL1 0x16
  46. #define HI3110_WRITE_INTE 0x1C
  47. #define HI3110_WRITE_BTR0 0x18
  48. #define HI3110_WRITE_BTR1 0x1A
  49. #define HI3110_READ_BTR0 0xD6
  50. #define HI3110_READ_BTR1 0xD8
  51. #define HI3110_READ_INTF 0xDE
  52. #define HI3110_READ_ERR 0xDC
  53. #define HI3110_READ_FIFO_WOTIME 0x48
  54. #define HI3110_WRITE_FIFO 0x12
  55. #define HI3110_READ_MESSTAT 0xDA
  56. #define HI3110_READ_REC 0xEA
  57. #define HI3110_READ_TEC 0xEC
  58. #define HI3110_CTRL0_MODE_MASK (7 << 5)
  59. #define HI3110_CTRL0_NORMAL_MODE (0 << 5)
  60. #define HI3110_CTRL0_LOOPBACK_MODE (1 << 5)
  61. #define HI3110_CTRL0_MONITOR_MODE (2 << 5)
  62. #define HI3110_CTRL0_SLEEP_MODE (3 << 5)
  63. #define HI3110_CTRL0_INIT_MODE (4 << 5)
  64. #define HI3110_CTRL1_TXEN BIT(7)
  65. #define HI3110_INT_RXTMP BIT(7)
  66. #define HI3110_INT_RXFIFO BIT(6)
  67. #define HI3110_INT_TXCPLT BIT(5)
  68. #define HI3110_INT_BUSERR BIT(4)
  69. #define HI3110_INT_MCHG BIT(3)
  70. #define HI3110_INT_WAKEUP BIT(2)
  71. #define HI3110_INT_F1MESS BIT(1)
  72. #define HI3110_INT_F0MESS BIT(0)
  73. #define HI3110_ERR_BUSOFF BIT(7)
  74. #define HI3110_ERR_TXERRP BIT(6)
  75. #define HI3110_ERR_RXERRP BIT(5)
  76. #define HI3110_ERR_BITERR BIT(4)
  77. #define HI3110_ERR_FRMERR BIT(3)
  78. #define HI3110_ERR_CRCERR BIT(2)
  79. #define HI3110_ERR_ACKERR BIT(1)
  80. #define HI3110_ERR_STUFERR BIT(0)
  81. #define HI3110_ERR_PROTOCOL_MASK (0x1F)
  82. #define HI3110_ERR_PASSIVE_MASK (0x60)
  83. #define HI3110_STAT_RXFMTY BIT(1)
  84. #define HI3110_STAT_BUSOFF BIT(2)
  85. #define HI3110_STAT_ERRP BIT(3)
  86. #define HI3110_STAT_ERRW BIT(4)
  87. #define HI3110_BTR0_SJW_SHIFT 6
  88. #define HI3110_BTR0_BRP_SHIFT 0
  89. #define HI3110_BTR1_SAMP_3PERBIT (1 << 7)
  90. #define HI3110_BTR1_SAMP_1PERBIT (0 << 7)
  91. #define HI3110_BTR1_TSEG2_SHIFT 4
  92. #define HI3110_BTR1_TSEG1_SHIFT 0
  93. #define HI3110_FIFO_WOTIME_TAG_OFF 0
  94. #define HI3110_FIFO_WOTIME_ID_OFF 1
  95. #define HI3110_FIFO_WOTIME_DLC_OFF 5
  96. #define HI3110_FIFO_WOTIME_DAT_OFF 6
  97. #define HI3110_FIFO_WOTIME_TAG_IDE BIT(7)
  98. #define HI3110_FIFO_WOTIME_ID_RTR BIT(0)
  99. #define HI3110_FIFO_TAG_OFF 0
  100. #define HI3110_FIFO_ID_OFF 1
  101. #define HI3110_FIFO_STD_DLC_OFF 3
  102. #define HI3110_FIFO_STD_DATA_OFF 4
  103. #define HI3110_FIFO_EXT_DLC_OFF 5
  104. #define HI3110_FIFO_EXT_DATA_OFF 6
  105. #define HI3110_CAN_MAX_DATA_LEN 8
  106. #define HI3110_RX_BUF_LEN 15
  107. #define HI3110_TX_STD_BUF_LEN 12
  108. #define HI3110_TX_EXT_BUF_LEN 14
  109. #define HI3110_CAN_FRAME_MAX_BITS 128
  110. #define HI3110_EFF_FLAGS 0x18 /* IDE + SRR */
  111. #define HI3110_TX_ECHO_SKB_MAX 1
  112. #define HI3110_OST_DELAY_MS (10)
  113. #define DEVICE_NAME "hi3110"
  114. static int hi3110_enable_dma = 1; /* Enable SPI DMA. Default: 1 (On) */
  115. module_param(hi3110_enable_dma, int, 0444);
  116. MODULE_PARM_DESC(hi3110_enable_dma, "Enable SPI DMA. Default: 1 (On)");
  117. static const struct can_bittiming_const hi3110_bittiming_const = {
  118. .name = DEVICE_NAME,
  119. .tseg1_min = 2,
  120. .tseg1_max = 16,
  121. .tseg2_min = 2,
  122. .tseg2_max = 8,
  123. .sjw_max = 4,
  124. .brp_min = 1,
  125. .brp_max = 64,
  126. .brp_inc = 1,
  127. };
  128. enum hi3110_model {
  129. CAN_HI3110_HI3110 = 0x3110,
  130. };
  131. struct hi3110_priv {
  132. struct can_priv can;
  133. struct net_device *net;
  134. struct spi_device *spi;
  135. enum hi3110_model model;
  136. struct mutex hi3110_lock; /* SPI device lock */
  137. u8 *spi_tx_buf;
  138. u8 *spi_rx_buf;
  139. dma_addr_t spi_tx_dma;
  140. dma_addr_t spi_rx_dma;
  141. struct sk_buff *tx_skb;
  142. int tx_len;
  143. struct workqueue_struct *wq;
  144. struct work_struct tx_work;
  145. struct work_struct restart_work;
  146. int force_quit;
  147. int after_suspend;
  148. #define HI3110_AFTER_SUSPEND_UP 1
  149. #define HI3110_AFTER_SUSPEND_DOWN 2
  150. #define HI3110_AFTER_SUSPEND_POWER 4
  151. #define HI3110_AFTER_SUSPEND_RESTART 8
  152. int restart_tx;
  153. struct regulator *power;
  154. struct regulator *transceiver;
  155. struct clk *clk;
  156. };
  157. static void hi3110_clean(struct net_device *net)
  158. {
  159. struct hi3110_priv *priv = netdev_priv(net);
  160. if (priv->tx_skb || priv->tx_len)
  161. net->stats.tx_errors++;
  162. if (priv->tx_skb)
  163. dev_kfree_skb(priv->tx_skb);
  164. if (priv->tx_len)
  165. can_free_echo_skb(priv->net, 0);
  166. priv->tx_skb = NULL;
  167. priv->tx_len = 0;
  168. }
  169. /* Note about handling of error return of hi3110_spi_trans: accessing
  170. * registers via SPI is not really different conceptually than using
  171. * normal I/O assembler instructions, although it's much more
  172. * complicated from a practical POV. So it's not advisable to always
  173. * check the return value of this function. Imagine that every
  174. * read{b,l}, write{b,l} and friends would be bracketed in "if ( < 0)
  175. * error();", it would be a great mess (well there are some situation
  176. * when exception handling C++ like could be useful after all). So we
  177. * just check that transfers are OK at the beginning of our
  178. * conversation with the chip and to avoid doing really nasty things
  179. * (like injecting bogus packets in the network stack).
  180. */
  181. static int hi3110_spi_trans(struct spi_device *spi, int len)
  182. {
  183. struct hi3110_priv *priv = spi_get_drvdata(spi);
  184. struct spi_transfer t = {
  185. .tx_buf = priv->spi_tx_buf,
  186. .rx_buf = priv->spi_rx_buf,
  187. .len = len,
  188. .cs_change = 0,
  189. };
  190. struct spi_message m;
  191. int ret;
  192. spi_message_init(&m);
  193. if (hi3110_enable_dma) {
  194. t.tx_dma = priv->spi_tx_dma;
  195. t.rx_dma = priv->spi_rx_dma;
  196. m.is_dma_mapped = 1;
  197. }
  198. spi_message_add_tail(&t, &m);
  199. ret = spi_sync(spi, &m);
  200. if (ret)
  201. dev_err(&spi->dev, "spi transfer failed: ret = %d\n", ret);
  202. return ret;
  203. }
  204. static u8 hi3110_cmd(struct spi_device *spi, u8 command)
  205. {
  206. struct hi3110_priv *priv = spi_get_drvdata(spi);
  207. priv->spi_tx_buf[0] = command;
  208. dev_dbg(&spi->dev, "hi3110_cmd: %02X\n", command);
  209. return hi3110_spi_trans(spi, 1);
  210. }
  211. static u8 hi3110_read(struct spi_device *spi, u8 command)
  212. {
  213. struct hi3110_priv *priv = spi_get_drvdata(spi);
  214. u8 val = 0;
  215. priv->spi_tx_buf[0] = command;
  216. hi3110_spi_trans(spi, 2);
  217. val = priv->spi_rx_buf[1];
  218. return val;
  219. }
  220. static void hi3110_write(struct spi_device *spi, u8 reg, u8 val)
  221. {
  222. struct hi3110_priv *priv = spi_get_drvdata(spi);
  223. priv->spi_tx_buf[0] = reg;
  224. priv->spi_tx_buf[1] = val;
  225. hi3110_spi_trans(spi, 2);
  226. }
  227. static void hi3110_hw_tx_frame(struct spi_device *spi, u8 *buf, int len)
  228. {
  229. struct hi3110_priv *priv = spi_get_drvdata(spi);
  230. priv->spi_tx_buf[0] = HI3110_WRITE_FIFO;
  231. memcpy(priv->spi_tx_buf + 1, buf, len);
  232. hi3110_spi_trans(spi, len + 1);
  233. }
  234. static void hi3110_hw_tx(struct spi_device *spi, struct can_frame *frame)
  235. {
  236. u8 buf[HI3110_TX_EXT_BUF_LEN];
  237. buf[HI3110_FIFO_TAG_OFF] = 0;
  238. if (frame->can_id & CAN_EFF_FLAG) {
  239. /* Extended frame */
  240. buf[HI3110_FIFO_ID_OFF] = (frame->can_id & CAN_EFF_MASK) >> 21;
  241. buf[HI3110_FIFO_ID_OFF + 1] =
  242. (((frame->can_id & CAN_EFF_MASK) >> 13) & 0xe0) |
  243. HI3110_EFF_FLAGS |
  244. (((frame->can_id & CAN_EFF_MASK) >> 15) & 0x07);
  245. buf[HI3110_FIFO_ID_OFF + 2] =
  246. (frame->can_id & CAN_EFF_MASK) >> 7;
  247. buf[HI3110_FIFO_ID_OFF + 3] =
  248. ((frame->can_id & CAN_EFF_MASK) << 1) |
  249. ((frame->can_id & CAN_RTR_FLAG) ? 1 : 0);
  250. buf[HI3110_FIFO_EXT_DLC_OFF] = frame->can_dlc;
  251. memcpy(buf + HI3110_FIFO_EXT_DATA_OFF,
  252. frame->data, frame->can_dlc);
  253. hi3110_hw_tx_frame(spi, buf, HI3110_TX_EXT_BUF_LEN -
  254. (HI3110_CAN_MAX_DATA_LEN - frame->can_dlc));
  255. } else {
  256. /* Standard frame */
  257. buf[HI3110_FIFO_ID_OFF] = (frame->can_id & CAN_SFF_MASK) >> 3;
  258. buf[HI3110_FIFO_ID_OFF + 1] =
  259. ((frame->can_id & CAN_SFF_MASK) << 5) |
  260. ((frame->can_id & CAN_RTR_FLAG) ? (1 << 4) : 0);
  261. buf[HI3110_FIFO_STD_DLC_OFF] = frame->can_dlc;
  262. memcpy(buf + HI3110_FIFO_STD_DATA_OFF,
  263. frame->data, frame->can_dlc);
  264. hi3110_hw_tx_frame(spi, buf, HI3110_TX_STD_BUF_LEN -
  265. (HI3110_CAN_MAX_DATA_LEN - frame->can_dlc));
  266. }
  267. }
  268. static void hi3110_hw_rx_frame(struct spi_device *spi, u8 *buf)
  269. {
  270. struct hi3110_priv *priv = spi_get_drvdata(spi);
  271. priv->spi_tx_buf[0] = HI3110_READ_FIFO_WOTIME;
  272. hi3110_spi_trans(spi, HI3110_RX_BUF_LEN);
  273. memcpy(buf, priv->spi_rx_buf + 1, HI3110_RX_BUF_LEN - 1);
  274. }
  275. static void hi3110_hw_rx(struct spi_device *spi)
  276. {
  277. struct hi3110_priv *priv = spi_get_drvdata(spi);
  278. struct sk_buff *skb;
  279. struct can_frame *frame;
  280. u8 buf[HI3110_RX_BUF_LEN - 1];
  281. skb = alloc_can_skb(priv->net, &frame);
  282. if (!skb) {
  283. priv->net->stats.rx_dropped++;
  284. return;
  285. }
  286. hi3110_hw_rx_frame(spi, buf);
  287. if (buf[HI3110_FIFO_WOTIME_TAG_OFF] & HI3110_FIFO_WOTIME_TAG_IDE) {
  288. /* IDE is recessive (1), indicating extended 29-bit frame */
  289. frame->can_id = CAN_EFF_FLAG;
  290. frame->can_id |=
  291. (buf[HI3110_FIFO_WOTIME_ID_OFF] << 21) |
  292. (((buf[HI3110_FIFO_WOTIME_ID_OFF + 1] & 0xE0) >> 5) << 18) |
  293. ((buf[HI3110_FIFO_WOTIME_ID_OFF + 1] & 0x07) << 15) |
  294. (buf[HI3110_FIFO_WOTIME_ID_OFF + 2] << 7) |
  295. (buf[HI3110_FIFO_WOTIME_ID_OFF + 3] >> 1);
  296. } else {
  297. /* IDE is dominant (0), frame indicating standard 11-bit */
  298. frame->can_id =
  299. (buf[HI3110_FIFO_WOTIME_ID_OFF] << 3) |
  300. ((buf[HI3110_FIFO_WOTIME_ID_OFF + 1] & 0xE0) >> 5);
  301. }
  302. /* Data length */
  303. frame->can_dlc = get_can_dlc(buf[HI3110_FIFO_WOTIME_DLC_OFF] & 0x0F);
  304. if (buf[HI3110_FIFO_WOTIME_ID_OFF + 3] & HI3110_FIFO_WOTIME_ID_RTR)
  305. frame->can_id |= CAN_RTR_FLAG;
  306. else
  307. memcpy(frame->data, buf + HI3110_FIFO_WOTIME_DAT_OFF,
  308. frame->can_dlc);
  309. priv->net->stats.rx_packets++;
  310. priv->net->stats.rx_bytes += frame->can_dlc;
  311. can_led_event(priv->net, CAN_LED_EVENT_RX);
  312. netif_rx_ni(skb);
  313. }
  314. static void hi3110_hw_sleep(struct spi_device *spi)
  315. {
  316. hi3110_write(spi, HI3110_WRITE_CTRL0, HI3110_CTRL0_SLEEP_MODE);
  317. }
  318. static netdev_tx_t hi3110_hard_start_xmit(struct sk_buff *skb,
  319. struct net_device *net)
  320. {
  321. struct hi3110_priv *priv = netdev_priv(net);
  322. struct spi_device *spi = priv->spi;
  323. if (priv->tx_skb || priv->tx_len) {
  324. dev_err(&spi->dev, "hard_xmit called while tx busy\n");
  325. return NETDEV_TX_BUSY;
  326. }
  327. if (can_dropped_invalid_skb(net, skb))
  328. return NETDEV_TX_OK;
  329. netif_stop_queue(net);
  330. priv->tx_skb = skb;
  331. queue_work(priv->wq, &priv->tx_work);
  332. return NETDEV_TX_OK;
  333. }
  334. static int hi3110_do_set_mode(struct net_device *net, enum can_mode mode)
  335. {
  336. struct hi3110_priv *priv = netdev_priv(net);
  337. switch (mode) {
  338. case CAN_MODE_START:
  339. hi3110_clean(net);
  340. /* We have to delay work since SPI I/O may sleep */
  341. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  342. priv->restart_tx = 1;
  343. if (priv->can.restart_ms == 0)
  344. priv->after_suspend = HI3110_AFTER_SUSPEND_RESTART;
  345. queue_work(priv->wq, &priv->restart_work);
  346. break;
  347. default:
  348. return -EOPNOTSUPP;
  349. }
  350. return 0;
  351. }
  352. static int hi3110_get_berr_counter(const struct net_device *net,
  353. struct can_berr_counter *bec)
  354. {
  355. struct hi3110_priv *priv = netdev_priv(net);
  356. struct spi_device *spi = priv->spi;
  357. bec->txerr = hi3110_read(spi, HI3110_READ_TEC);
  358. bec->rxerr = hi3110_read(spi, HI3110_READ_REC);
  359. return 0;
  360. }
  361. static int hi3110_set_normal_mode(struct spi_device *spi)
  362. {
  363. struct hi3110_priv *priv = spi_get_drvdata(spi);
  364. u8 reg = 0;
  365. hi3110_write(spi, HI3110_WRITE_INTE, HI3110_INT_BUSERR |
  366. HI3110_INT_RXFIFO | HI3110_INT_TXCPLT);
  367. /* Enable TX */
  368. hi3110_write(spi, HI3110_WRITE_CTRL1, HI3110_CTRL1_TXEN);
  369. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  370. reg = HI3110_CTRL0_LOOPBACK_MODE;
  371. else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  372. reg = HI3110_CTRL0_MONITOR_MODE;
  373. else
  374. reg = HI3110_CTRL0_NORMAL_MODE;
  375. hi3110_write(spi, HI3110_WRITE_CTRL0, reg);
  376. /* Wait for the device to enter the mode */
  377. mdelay(HI3110_OST_DELAY_MS);
  378. reg = hi3110_read(spi, HI3110_READ_CTRL0);
  379. if ((reg & HI3110_CTRL0_MODE_MASK) != reg)
  380. return -EBUSY;
  381. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  382. return 0;
  383. }
  384. static int hi3110_do_set_bittiming(struct net_device *net)
  385. {
  386. struct hi3110_priv *priv = netdev_priv(net);
  387. struct can_bittiming *bt = &priv->can.bittiming;
  388. struct spi_device *spi = priv->spi;
  389. hi3110_write(spi, HI3110_WRITE_BTR0,
  390. ((bt->sjw - 1) << HI3110_BTR0_SJW_SHIFT) |
  391. ((bt->brp - 1) << HI3110_BTR0_BRP_SHIFT));
  392. hi3110_write(spi, HI3110_WRITE_BTR1,
  393. (priv->can.ctrlmode &
  394. CAN_CTRLMODE_3_SAMPLES ?
  395. HI3110_BTR1_SAMP_3PERBIT : HI3110_BTR1_SAMP_1PERBIT) |
  396. ((bt->phase_seg1 + bt->prop_seg - 1)
  397. << HI3110_BTR1_TSEG1_SHIFT) |
  398. ((bt->phase_seg2 - 1) << HI3110_BTR1_TSEG2_SHIFT));
  399. dev_dbg(&spi->dev, "BT: 0x%02x 0x%02x\n",
  400. hi3110_read(spi, HI3110_READ_BTR0),
  401. hi3110_read(spi, HI3110_READ_BTR1));
  402. return 0;
  403. }
  404. static int hi3110_setup(struct net_device *net)
  405. {
  406. hi3110_do_set_bittiming(net);
  407. return 0;
  408. }
  409. static int hi3110_hw_reset(struct spi_device *spi)
  410. {
  411. u8 reg;
  412. int ret;
  413. /* Wait for oscillator startup timer after power up */
  414. mdelay(HI3110_OST_DELAY_MS);
  415. ret = hi3110_cmd(spi, HI3110_MASTER_RESET);
  416. if (ret)
  417. return ret;
  418. /* Wait for oscillator startup timer after reset */
  419. mdelay(HI3110_OST_DELAY_MS);
  420. reg = hi3110_read(spi, HI3110_READ_CTRL0);
  421. if ((reg & HI3110_CTRL0_MODE_MASK) != HI3110_CTRL0_INIT_MODE)
  422. return -ENODEV;
  423. /* As per the datasheet it appears the error flags are
  424. * not cleared on reset. Explicitly clear them by performing a read
  425. */
  426. hi3110_read(spi, HI3110_READ_ERR);
  427. return 0;
  428. }
  429. static int hi3110_hw_probe(struct spi_device *spi)
  430. {
  431. u8 statf;
  432. hi3110_hw_reset(spi);
  433. /* Confirm correct operation by checking against reset values
  434. * in datasheet
  435. */
  436. statf = hi3110_read(spi, HI3110_READ_STATF);
  437. dev_dbg(&spi->dev, "statf: %02X\n", statf);
  438. if (statf != 0x82)
  439. return -ENODEV;
  440. return 0;
  441. }
  442. static int hi3110_power_enable(struct regulator *reg, int enable)
  443. {
  444. if (IS_ERR_OR_NULL(reg))
  445. return 0;
  446. if (enable)
  447. return regulator_enable(reg);
  448. else
  449. return regulator_disable(reg);
  450. }
  451. static int hi3110_stop(struct net_device *net)
  452. {
  453. struct hi3110_priv *priv = netdev_priv(net);
  454. struct spi_device *spi = priv->spi;
  455. close_candev(net);
  456. priv->force_quit = 1;
  457. free_irq(spi->irq, priv);
  458. destroy_workqueue(priv->wq);
  459. priv->wq = NULL;
  460. mutex_lock(&priv->hi3110_lock);
  461. /* Disable transmit, interrupts and clear flags */
  462. hi3110_write(spi, HI3110_WRITE_CTRL1, 0x0);
  463. hi3110_write(spi, HI3110_WRITE_INTE, 0x0);
  464. hi3110_read(spi, HI3110_READ_INTF);
  465. hi3110_clean(net);
  466. hi3110_hw_sleep(spi);
  467. hi3110_power_enable(priv->transceiver, 0);
  468. priv->can.state = CAN_STATE_STOPPED;
  469. mutex_unlock(&priv->hi3110_lock);
  470. can_led_event(net, CAN_LED_EVENT_STOP);
  471. return 0;
  472. }
  473. static void hi3110_tx_work_handler(struct work_struct *ws)
  474. {
  475. struct hi3110_priv *priv = container_of(ws, struct hi3110_priv,
  476. tx_work);
  477. struct spi_device *spi = priv->spi;
  478. struct net_device *net = priv->net;
  479. struct can_frame *frame;
  480. mutex_lock(&priv->hi3110_lock);
  481. if (priv->tx_skb) {
  482. if (priv->can.state == CAN_STATE_BUS_OFF) {
  483. hi3110_clean(net);
  484. } else {
  485. frame = (struct can_frame *)priv->tx_skb->data;
  486. hi3110_hw_tx(spi, frame);
  487. priv->tx_len = 1 + frame->can_dlc;
  488. can_put_echo_skb(priv->tx_skb, net, 0);
  489. priv->tx_skb = NULL;
  490. }
  491. }
  492. mutex_unlock(&priv->hi3110_lock);
  493. }
  494. static void hi3110_restart_work_handler(struct work_struct *ws)
  495. {
  496. struct hi3110_priv *priv = container_of(ws, struct hi3110_priv,
  497. restart_work);
  498. struct spi_device *spi = priv->spi;
  499. struct net_device *net = priv->net;
  500. mutex_lock(&priv->hi3110_lock);
  501. if (priv->after_suspend) {
  502. hi3110_hw_reset(spi);
  503. hi3110_setup(net);
  504. if (priv->after_suspend & HI3110_AFTER_SUSPEND_RESTART) {
  505. hi3110_set_normal_mode(spi);
  506. } else if (priv->after_suspend & HI3110_AFTER_SUSPEND_UP) {
  507. netif_device_attach(net);
  508. hi3110_clean(net);
  509. hi3110_set_normal_mode(spi);
  510. netif_wake_queue(net);
  511. } else {
  512. hi3110_hw_sleep(spi);
  513. }
  514. priv->after_suspend = 0;
  515. priv->force_quit = 0;
  516. }
  517. if (priv->restart_tx) {
  518. priv->restart_tx = 0;
  519. hi3110_hw_reset(spi);
  520. hi3110_setup(net);
  521. hi3110_clean(net);
  522. hi3110_set_normal_mode(spi);
  523. netif_wake_queue(net);
  524. }
  525. mutex_unlock(&priv->hi3110_lock);
  526. }
  527. static irqreturn_t hi3110_can_ist(int irq, void *dev_id)
  528. {
  529. struct hi3110_priv *priv = dev_id;
  530. struct spi_device *spi = priv->spi;
  531. struct net_device *net = priv->net;
  532. mutex_lock(&priv->hi3110_lock);
  533. while (!priv->force_quit) {
  534. enum can_state new_state;
  535. u8 intf, eflag, statf;
  536. while (!(HI3110_STAT_RXFMTY &
  537. (statf = hi3110_read(spi, HI3110_READ_STATF)))) {
  538. hi3110_hw_rx(spi);
  539. }
  540. intf = hi3110_read(spi, HI3110_READ_INTF);
  541. eflag = hi3110_read(spi, HI3110_READ_ERR);
  542. /* Update can state */
  543. if (eflag & HI3110_ERR_BUSOFF)
  544. new_state = CAN_STATE_BUS_OFF;
  545. else if (eflag & HI3110_ERR_PASSIVE_MASK)
  546. new_state = CAN_STATE_ERROR_PASSIVE;
  547. else if (statf & HI3110_STAT_ERRW)
  548. new_state = CAN_STATE_ERROR_WARNING;
  549. else
  550. new_state = CAN_STATE_ERROR_ACTIVE;
  551. if (new_state != priv->can.state) {
  552. struct can_frame *cf;
  553. struct sk_buff *skb;
  554. enum can_state rx_state, tx_state;
  555. u8 rxerr, txerr;
  556. skb = alloc_can_err_skb(net, &cf);
  557. if (!skb)
  558. break;
  559. txerr = hi3110_read(spi, HI3110_READ_TEC);
  560. rxerr = hi3110_read(spi, HI3110_READ_REC);
  561. cf->data[6] = txerr;
  562. cf->data[7] = rxerr;
  563. tx_state = txerr >= rxerr ? new_state : 0;
  564. rx_state = txerr <= rxerr ? new_state : 0;
  565. can_change_state(net, cf, tx_state, rx_state);
  566. netif_rx_ni(skb);
  567. if (new_state == CAN_STATE_BUS_OFF) {
  568. can_bus_off(net);
  569. if (priv->can.restart_ms == 0) {
  570. priv->force_quit = 1;
  571. hi3110_hw_sleep(spi);
  572. break;
  573. }
  574. }
  575. }
  576. /* Update bus errors */
  577. if ((intf & HI3110_INT_BUSERR) &&
  578. (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) {
  579. struct can_frame *cf;
  580. struct sk_buff *skb;
  581. /* Check for protocol errors */
  582. if (eflag & HI3110_ERR_PROTOCOL_MASK) {
  583. skb = alloc_can_err_skb(net, &cf);
  584. if (!skb)
  585. break;
  586. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  587. priv->can.can_stats.bus_error++;
  588. priv->net->stats.rx_errors++;
  589. if (eflag & HI3110_ERR_BITERR)
  590. cf->data[2] |= CAN_ERR_PROT_BIT;
  591. else if (eflag & HI3110_ERR_FRMERR)
  592. cf->data[2] |= CAN_ERR_PROT_FORM;
  593. else if (eflag & HI3110_ERR_STUFERR)
  594. cf->data[2] |= CAN_ERR_PROT_STUFF;
  595. else if (eflag & HI3110_ERR_CRCERR)
  596. cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
  597. else if (eflag & HI3110_ERR_ACKERR)
  598. cf->data[3] |= CAN_ERR_PROT_LOC_ACK;
  599. cf->data[6] = hi3110_read(spi, HI3110_READ_TEC);
  600. cf->data[7] = hi3110_read(spi, HI3110_READ_REC);
  601. netdev_dbg(priv->net, "Bus Error\n");
  602. netif_rx_ni(skb);
  603. }
  604. }
  605. if (intf == 0)
  606. break;
  607. if (intf & HI3110_INT_TXCPLT) {
  608. net->stats.tx_packets++;
  609. net->stats.tx_bytes += priv->tx_len - 1;
  610. can_led_event(net, CAN_LED_EVENT_TX);
  611. if (priv->tx_len) {
  612. can_get_echo_skb(net, 0);
  613. priv->tx_len = 0;
  614. }
  615. netif_wake_queue(net);
  616. }
  617. }
  618. mutex_unlock(&priv->hi3110_lock);
  619. return IRQ_HANDLED;
  620. }
  621. static int hi3110_open(struct net_device *net)
  622. {
  623. struct hi3110_priv *priv = netdev_priv(net);
  624. struct spi_device *spi = priv->spi;
  625. unsigned long flags = IRQF_ONESHOT | IRQF_TRIGGER_RISING;
  626. int ret;
  627. ret = open_candev(net);
  628. if (ret)
  629. return ret;
  630. mutex_lock(&priv->hi3110_lock);
  631. hi3110_power_enable(priv->transceiver, 1);
  632. priv->force_quit = 0;
  633. priv->tx_skb = NULL;
  634. priv->tx_len = 0;
  635. ret = request_threaded_irq(spi->irq, NULL, hi3110_can_ist,
  636. flags, DEVICE_NAME, priv);
  637. if (ret) {
  638. dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq);
  639. goto out_close;
  640. }
  641. priv->wq = alloc_workqueue("hi3110_wq", WQ_FREEZABLE | WQ_MEM_RECLAIM,
  642. 0);
  643. if (!priv->wq) {
  644. ret = -ENOMEM;
  645. goto out_free_irq;
  646. }
  647. INIT_WORK(&priv->tx_work, hi3110_tx_work_handler);
  648. INIT_WORK(&priv->restart_work, hi3110_restart_work_handler);
  649. ret = hi3110_hw_reset(spi);
  650. if (ret)
  651. goto out_free_wq;
  652. ret = hi3110_setup(net);
  653. if (ret)
  654. goto out_free_wq;
  655. ret = hi3110_set_normal_mode(spi);
  656. if (ret)
  657. goto out_free_wq;
  658. can_led_event(net, CAN_LED_EVENT_OPEN);
  659. netif_wake_queue(net);
  660. mutex_unlock(&priv->hi3110_lock);
  661. return 0;
  662. out_free_wq:
  663. destroy_workqueue(priv->wq);
  664. out_free_irq:
  665. free_irq(spi->irq, priv);
  666. hi3110_hw_sleep(spi);
  667. out_close:
  668. hi3110_power_enable(priv->transceiver, 0);
  669. close_candev(net);
  670. mutex_unlock(&priv->hi3110_lock);
  671. return ret;
  672. }
  673. static const struct net_device_ops hi3110_netdev_ops = {
  674. .ndo_open = hi3110_open,
  675. .ndo_stop = hi3110_stop,
  676. .ndo_start_xmit = hi3110_hard_start_xmit,
  677. };
  678. static const struct of_device_id hi3110_of_match[] = {
  679. {
  680. .compatible = "holt,hi3110",
  681. .data = (void *)CAN_HI3110_HI3110,
  682. },
  683. { }
  684. };
  685. MODULE_DEVICE_TABLE(of, hi3110_of_match);
  686. static const struct spi_device_id hi3110_id_table[] = {
  687. {
  688. .name = "hi3110",
  689. .driver_data = (kernel_ulong_t)CAN_HI3110_HI3110,
  690. },
  691. { }
  692. };
  693. MODULE_DEVICE_TABLE(spi, hi3110_id_table);
  694. static int hi3110_can_probe(struct spi_device *spi)
  695. {
  696. const struct of_device_id *of_id = of_match_device(hi3110_of_match,
  697. &spi->dev);
  698. struct net_device *net;
  699. struct hi3110_priv *priv;
  700. struct clk *clk;
  701. int freq, ret;
  702. clk = devm_clk_get(&spi->dev, NULL);
  703. if (IS_ERR(clk)) {
  704. dev_err(&spi->dev, "no CAN clock source defined\n");
  705. return PTR_ERR(clk);
  706. }
  707. freq = clk_get_rate(clk);
  708. /* Sanity check */
  709. if (freq > 40000000)
  710. return -ERANGE;
  711. /* Allocate can/net device */
  712. net = alloc_candev(sizeof(struct hi3110_priv), HI3110_TX_ECHO_SKB_MAX);
  713. if (!net)
  714. return -ENOMEM;
  715. if (!IS_ERR(clk)) {
  716. ret = clk_prepare_enable(clk);
  717. if (ret)
  718. goto out_free;
  719. }
  720. net->netdev_ops = &hi3110_netdev_ops;
  721. net->flags |= IFF_ECHO;
  722. priv = netdev_priv(net);
  723. priv->can.bittiming_const = &hi3110_bittiming_const;
  724. priv->can.do_set_mode = hi3110_do_set_mode;
  725. priv->can.do_get_berr_counter = hi3110_get_berr_counter;
  726. priv->can.clock.freq = freq / 2;
  727. priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
  728. CAN_CTRLMODE_LOOPBACK |
  729. CAN_CTRLMODE_LISTENONLY |
  730. CAN_CTRLMODE_BERR_REPORTING;
  731. if (of_id)
  732. priv->model = (enum hi3110_model)of_id->data;
  733. else
  734. priv->model = spi_get_device_id(spi)->driver_data;
  735. priv->net = net;
  736. priv->clk = clk;
  737. spi_set_drvdata(spi, priv);
  738. /* Configure the SPI bus */
  739. spi->bits_per_word = 8;
  740. ret = spi_setup(spi);
  741. if (ret)
  742. goto out_clk;
  743. priv->power = devm_regulator_get_optional(&spi->dev, "vdd");
  744. priv->transceiver = devm_regulator_get_optional(&spi->dev, "xceiver");
  745. if ((PTR_ERR(priv->power) == -EPROBE_DEFER) ||
  746. (PTR_ERR(priv->transceiver) == -EPROBE_DEFER)) {
  747. ret = -EPROBE_DEFER;
  748. goto out_clk;
  749. }
  750. ret = hi3110_power_enable(priv->power, 1);
  751. if (ret)
  752. goto out_clk;
  753. priv->spi = spi;
  754. mutex_init(&priv->hi3110_lock);
  755. /* If requested, allocate DMA buffers */
  756. if (hi3110_enable_dma) {
  757. spi->dev.coherent_dma_mask = ~0;
  758. /* Minimum coherent DMA allocation is PAGE_SIZE, so allocate
  759. * that much and share it between Tx and Rx DMA buffers.
  760. */
  761. priv->spi_tx_buf = dmam_alloc_coherent(&spi->dev,
  762. PAGE_SIZE,
  763. &priv->spi_tx_dma,
  764. GFP_DMA);
  765. if (priv->spi_tx_buf) {
  766. priv->spi_rx_buf = (priv->spi_tx_buf + (PAGE_SIZE / 2));
  767. priv->spi_rx_dma = (dma_addr_t)(priv->spi_tx_dma +
  768. (PAGE_SIZE / 2));
  769. } else {
  770. /* Fall back to non-DMA */
  771. hi3110_enable_dma = 0;
  772. }
  773. }
  774. /* Allocate non-DMA buffers */
  775. if (!hi3110_enable_dma) {
  776. priv->spi_tx_buf = devm_kzalloc(&spi->dev, HI3110_RX_BUF_LEN,
  777. GFP_KERNEL);
  778. if (!priv->spi_tx_buf) {
  779. ret = -ENOMEM;
  780. goto error_probe;
  781. }
  782. priv->spi_rx_buf = devm_kzalloc(&spi->dev, HI3110_RX_BUF_LEN,
  783. GFP_KERNEL);
  784. if (!priv->spi_rx_buf) {
  785. ret = -ENOMEM;
  786. goto error_probe;
  787. }
  788. }
  789. SET_NETDEV_DEV(net, &spi->dev);
  790. ret = hi3110_hw_probe(spi);
  791. if (ret) {
  792. if (ret == -ENODEV)
  793. dev_err(&spi->dev, "Cannot initialize %x. Wrong wiring?\n",
  794. priv->model);
  795. goto error_probe;
  796. }
  797. hi3110_hw_sleep(spi);
  798. ret = register_candev(net);
  799. if (ret)
  800. goto error_probe;
  801. devm_can_led_init(net);
  802. netdev_info(net, "%x successfully initialized.\n", priv->model);
  803. return 0;
  804. error_probe:
  805. hi3110_power_enable(priv->power, 0);
  806. out_clk:
  807. if (!IS_ERR(clk))
  808. clk_disable_unprepare(clk);
  809. out_free:
  810. free_candev(net);
  811. dev_err(&spi->dev, "Probe failed, err=%d\n", -ret);
  812. return ret;
  813. }
  814. static int hi3110_can_remove(struct spi_device *spi)
  815. {
  816. struct hi3110_priv *priv = spi_get_drvdata(spi);
  817. struct net_device *net = priv->net;
  818. unregister_candev(net);
  819. hi3110_power_enable(priv->power, 0);
  820. if (!IS_ERR(priv->clk))
  821. clk_disable_unprepare(priv->clk);
  822. free_candev(net);
  823. return 0;
  824. }
  825. static int __maybe_unused hi3110_can_suspend(struct device *dev)
  826. {
  827. struct spi_device *spi = to_spi_device(dev);
  828. struct hi3110_priv *priv = spi_get_drvdata(spi);
  829. struct net_device *net = priv->net;
  830. priv->force_quit = 1;
  831. disable_irq(spi->irq);
  832. /* Note: at this point neither IST nor workqueues are running.
  833. * open/stop cannot be called anyway so locking is not needed
  834. */
  835. if (netif_running(net)) {
  836. netif_device_detach(net);
  837. hi3110_hw_sleep(spi);
  838. hi3110_power_enable(priv->transceiver, 0);
  839. priv->after_suspend = HI3110_AFTER_SUSPEND_UP;
  840. } else {
  841. priv->after_suspend = HI3110_AFTER_SUSPEND_DOWN;
  842. }
  843. if (!IS_ERR_OR_NULL(priv->power)) {
  844. regulator_disable(priv->power);
  845. priv->after_suspend |= HI3110_AFTER_SUSPEND_POWER;
  846. }
  847. return 0;
  848. }
  849. static int __maybe_unused hi3110_can_resume(struct device *dev)
  850. {
  851. struct spi_device *spi = to_spi_device(dev);
  852. struct hi3110_priv *priv = spi_get_drvdata(spi);
  853. if (priv->after_suspend & HI3110_AFTER_SUSPEND_POWER)
  854. hi3110_power_enable(priv->power, 1);
  855. if (priv->after_suspend & HI3110_AFTER_SUSPEND_UP) {
  856. hi3110_power_enable(priv->transceiver, 1);
  857. queue_work(priv->wq, &priv->restart_work);
  858. } else {
  859. priv->after_suspend = 0;
  860. }
  861. priv->force_quit = 0;
  862. enable_irq(spi->irq);
  863. return 0;
  864. }
  865. static SIMPLE_DEV_PM_OPS(hi3110_can_pm_ops, hi3110_can_suspend, hi3110_can_resume);
  866. static struct spi_driver hi3110_can_driver = {
  867. .driver = {
  868. .name = DEVICE_NAME,
  869. .of_match_table = hi3110_of_match,
  870. .pm = &hi3110_can_pm_ops,
  871. },
  872. .id_table = hi3110_id_table,
  873. .probe = hi3110_can_probe,
  874. .remove = hi3110_can_remove,
  875. };
  876. module_spi_driver(hi3110_can_driver);
  877. MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
  878. MODULE_AUTHOR("Casey Fitzpatrick <casey.fitzpatrick@timesys.com>");
  879. MODULE_DESCRIPTION("Holt HI-3110 CAN driver");
  880. MODULE_LICENSE("GPL v2");