spi-sh-hspi.c 6.7 KB

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