hi311x.c 27 KB

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