spi-sh-hspi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * SuperH HSPI bus driver
  3. *
  4. * Copyright (C) 2011 Kuninori Morimoto
  5. *
  6. * Based on spi-sh.c:
  7. * Based on pxa2xx_spi.c:
  8. * Copyright (C) 2011 Renesas Solutions Corp.
  9. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/timer.h>
  24. #include <linux/delay.h>
  25. #include <linux/list.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/io.h>
  30. #include <linux/spi/spi.h>
  31. #include <linux/spi/sh_hspi.h>
  32. #define SPCR 0x00
  33. #define SPSR 0x04
  34. #define SPSCR 0x08
  35. #define SPTBR 0x0C
  36. #define SPRBR 0x10
  37. #define SPCR2 0x14
  38. /* SPSR */
  39. #define RXFL (1 << 2)
  40. struct hspi_priv {
  41. void __iomem *addr;
  42. struct spi_master *master;
  43. struct device *dev;
  44. struct clk *clk;
  45. };
  46. /*
  47. * basic function
  48. */
  49. static void hspi_write(struct hspi_priv *hspi, int reg, u32 val)
  50. {
  51. iowrite32(val, hspi->addr + reg);
  52. }
  53. static u32 hspi_read(struct hspi_priv *hspi, int reg)
  54. {
  55. return ioread32(hspi->addr + reg);
  56. }
  57. static void hspi_bit_set(struct hspi_priv *hspi, int reg, u32 mask, u32 set)
  58. {
  59. u32 val = hspi_read(hspi, reg);
  60. val &= ~mask;
  61. val |= set & mask;
  62. hspi_write(hspi, reg, val);
  63. }
  64. /*
  65. * transfer function
  66. */
  67. static int hspi_status_check_timeout(struct hspi_priv *hspi, u32 mask, u32 val)
  68. {
  69. int t = 256;
  70. while (t--) {
  71. if ((mask & hspi_read(hspi, SPSR)) == val)
  72. return 0;
  73. udelay(10);
  74. }
  75. dev_err(hspi->dev, "timeout\n");
  76. return -ETIMEDOUT;
  77. }
  78. /*
  79. * spi master function
  80. */
  81. #define hspi_hw_cs_enable(hspi) hspi_hw_cs_ctrl(hspi, 0)
  82. #define hspi_hw_cs_disable(hspi) hspi_hw_cs_ctrl(hspi, 1)
  83. static void hspi_hw_cs_ctrl(struct hspi_priv *hspi, int hi)
  84. {
  85. hspi_bit_set(hspi, SPSCR, (1 << 6), (hi) << 6);
  86. }
  87. static void hspi_hw_setup(struct hspi_priv *hspi,
  88. struct spi_message *msg,
  89. struct spi_transfer *t)
  90. {
  91. struct spi_device *spi = msg->spi;
  92. struct device *dev = hspi->dev;
  93. u32 spcr, idiv_clk;
  94. u32 rate, best_rate, min, tmp;
  95. /*
  96. * find best IDIV/CLKCx settings
  97. */
  98. min = ~0;
  99. best_rate = 0;
  100. spcr = 0;
  101. for (idiv_clk = 0x00; idiv_clk <= 0x3F; idiv_clk++) {
  102. rate = clk_get_rate(hspi->clk);
  103. /* IDIV calculation */
  104. if (idiv_clk & (1 << 5))
  105. rate /= 128;
  106. else
  107. rate /= 16;
  108. /* CLKCx calculation */
  109. rate /= (((idiv_clk & 0x1F) + 1) * 2);
  110. /* save best settings */
  111. tmp = abs(t->speed_hz - rate);
  112. if (tmp < min) {
  113. min = tmp;
  114. spcr = idiv_clk;
  115. best_rate = rate;
  116. }
  117. }
  118. if (spi->mode & SPI_CPHA)
  119. spcr |= 1 << 7;
  120. if (spi->mode & SPI_CPOL)
  121. spcr |= 1 << 6;
  122. dev_dbg(dev, "speed %d/%d\n", t->speed_hz, best_rate);
  123. hspi_write(hspi, SPCR, spcr);
  124. hspi_write(hspi, SPSR, 0x0);
  125. hspi_write(hspi, SPSCR, 0x21); /* master mode / CS control */
  126. }
  127. static int hspi_transfer_one_message(struct spi_master *master,
  128. struct spi_message *msg)
  129. {
  130. struct hspi_priv *hspi = spi_master_get_devdata(master);
  131. struct spi_transfer *t;
  132. u32 tx;
  133. u32 rx;
  134. int ret, i;
  135. unsigned int cs_change;
  136. const int nsecs = 50;
  137. dev_dbg(hspi->dev, "%s\n", __func__);
  138. cs_change = 1;
  139. ret = 0;
  140. list_for_each_entry(t, &msg->transfers, transfer_list) {
  141. if (cs_change) {
  142. hspi_hw_setup(hspi, msg, t);
  143. hspi_hw_cs_enable(hspi);
  144. ndelay(nsecs);
  145. }
  146. cs_change = t->cs_change;
  147. for (i = 0; i < t->len; i++) {
  148. /* wait remains */
  149. ret = hspi_status_check_timeout(hspi, 0x1, 0);
  150. if (ret < 0)
  151. break;
  152. tx = 0;
  153. if (t->tx_buf)
  154. tx = (u32)((u8 *)t->tx_buf)[i];
  155. hspi_write(hspi, SPTBR, tx);
  156. /* wait receive */
  157. ret = hspi_status_check_timeout(hspi, 0x4, 0x4);
  158. if (ret < 0)
  159. break;
  160. rx = hspi_read(hspi, SPRBR);
  161. if (t->rx_buf)
  162. ((u8 *)t->rx_buf)[i] = (u8)rx;
  163. }
  164. msg->actual_length += t->len;
  165. if (t->delay_usecs)
  166. udelay(t->delay_usecs);
  167. if (cs_change) {
  168. ndelay(nsecs);
  169. hspi_hw_cs_disable(hspi);
  170. ndelay(nsecs);
  171. }
  172. }
  173. msg->status = ret;
  174. if (!cs_change) {
  175. ndelay(nsecs);
  176. hspi_hw_cs_disable(hspi);
  177. }
  178. spi_finalize_current_message(master);
  179. return ret;
  180. }
  181. static int hspi_probe(struct platform_device *pdev)
  182. {
  183. struct resource *res;
  184. struct spi_master *master;
  185. struct hspi_priv *hspi;
  186. struct clk *clk;
  187. int ret;
  188. /* get base addr */
  189. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  190. if (!res) {
  191. dev_err(&pdev->dev, "invalid resource\n");
  192. return -EINVAL;
  193. }
  194. master = spi_alloc_master(&pdev->dev, sizeof(*hspi));
  195. if (!master) {
  196. dev_err(&pdev->dev, "spi_alloc_master error.\n");
  197. return -ENOMEM;
  198. }
  199. clk = clk_get(&pdev->dev, NULL);
  200. if (IS_ERR(clk)) {
  201. dev_err(&pdev->dev, "couldn't get clock\n");
  202. ret = -EINVAL;
  203. goto error0;
  204. }
  205. hspi = spi_master_get_devdata(master);
  206. platform_set_drvdata(pdev, hspi);
  207. /* init hspi */
  208. hspi->master = master;
  209. hspi->dev = &pdev->dev;
  210. hspi->clk = clk;
  211. hspi->addr = devm_ioremap(hspi->dev,
  212. res->start, resource_size(res));
  213. if (!hspi->addr) {
  214. dev_err(&pdev->dev, "ioremap error.\n");
  215. ret = -ENOMEM;
  216. goto error1;
  217. }
  218. pm_runtime_enable(&pdev->dev);
  219. master->bus_num = pdev->id;
  220. master->mode_bits = SPI_CPOL | SPI_CPHA;
  221. master->dev.of_node = pdev->dev.of_node;
  222. master->auto_runtime_pm = true;
  223. master->transfer_one_message = hspi_transfer_one_message;
  224. master->bits_per_word_mask = SPI_BPW_MASK(8);
  225. ret = devm_spi_register_master(&pdev->dev, master);
  226. if (ret < 0) {
  227. dev_err(&pdev->dev, "spi_register_master error.\n");
  228. goto error2;
  229. }
  230. return 0;
  231. error2:
  232. pm_runtime_disable(&pdev->dev);
  233. error1:
  234. clk_put(clk);
  235. error0:
  236. spi_master_put(master);
  237. return ret;
  238. }
  239. static int hspi_remove(struct platform_device *pdev)
  240. {
  241. struct hspi_priv *hspi = platform_get_drvdata(pdev);
  242. pm_runtime_disable(&pdev->dev);
  243. clk_put(hspi->clk);
  244. return 0;
  245. }
  246. static const struct of_device_id hspi_of_match[] = {
  247. { .compatible = "renesas,hspi", },
  248. { /* sentinel */ }
  249. };
  250. MODULE_DEVICE_TABLE(of, hspi_of_match);
  251. static struct platform_driver hspi_driver = {
  252. .probe = hspi_probe,
  253. .remove = hspi_remove,
  254. .driver = {
  255. .name = "sh-hspi",
  256. .of_match_table = hspi_of_match,
  257. },
  258. };
  259. module_platform_driver(hspi_driver);
  260. MODULE_DESCRIPTION("SuperH HSPI bus driver");
  261. MODULE_LICENSE("GPL");
  262. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  263. MODULE_ALIAS("platform:sh-hspi");