spi-phytium.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * Phytium SPI core controller driver.
  3. *
  4. * Copyright (c) 2019, Phytium Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/gpio.h>
  19. #include <linux/highmem.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/of_gpio.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/property.h>
  31. #include <linux/acpi.h>
  32. #define CTRL0 0x00
  33. #define SSIENR 0x08
  34. #define SER 0x10
  35. #define BAUDR 0x14
  36. #define TXFLTR 0x18
  37. #define TXFLR 0x20
  38. #define RXFLR 0x24
  39. #define IMR 0x2c
  40. #define ISR 0x30
  41. #define ICR 0x48
  42. #define DR 0x60
  43. #define FRF_OFFSET 4
  44. #define MODE_OFFSET 6
  45. #define TMOD_OFFSET 8
  46. #define TMOD_MASK (0x3 << TMOD_OFFSET)
  47. #define TMOD_TR 0x0
  48. #define TMOD_TO 0x1
  49. #define TMOD_RO 0x2
  50. #define INT_TXEI (1 << 0)
  51. #define INT_TXOI (1 << 1)
  52. #define INT_RXUI (1 << 2)
  53. #define INT_RXOI (1 << 3)
  54. struct ft_spi {
  55. struct spi_master *master;
  56. char name[16];
  57. void __iomem *regs;
  58. unsigned long paddr;
  59. int irq;
  60. u32 fifo_len;
  61. u32 max_freq;
  62. u32 reg_io_width;
  63. u16 bus_num;
  64. u16 num_cs;
  65. size_t len;
  66. void *tx;
  67. void *tx_end;
  68. void *rx;
  69. void *rx_end;
  70. u8 n_bytes;
  71. irqreturn_t (*transfer_handler)(struct ft_spi *fts);
  72. };
  73. static inline u32 ft_readl(struct ft_spi *fts, u32 offset)
  74. {
  75. return __raw_readl(fts->regs + offset);
  76. }
  77. static inline u16 ft_readw(struct ft_spi *fts, u32 offset)
  78. {
  79. return __raw_readw(fts->regs + offset);
  80. }
  81. static inline void ft_writel(struct ft_spi *fts, u32 offset, u32 val)
  82. {
  83. __raw_writel(val, fts->regs + offset);
  84. }
  85. static inline void ft_writew(struct ft_spi *fts, u32 offset, u16 val)
  86. {
  87. __raw_writew(val, fts->regs + offset);
  88. }
  89. static inline u32 ft_read_io_reg(struct ft_spi *fts, u32 offset)
  90. {
  91. switch (fts->reg_io_width) {
  92. case 2:
  93. return ft_readw(fts, offset);
  94. case 4:
  95. default:
  96. return ft_readl(fts, offset);
  97. }
  98. }
  99. static inline void ft_write_io_reg(struct ft_spi *fts, u32 offset, u32 val)
  100. {
  101. switch (fts->reg_io_width) {
  102. case 2:
  103. ft_writew(fts, offset, val);
  104. break;
  105. case 4:
  106. default:
  107. ft_writel(fts, offset, val);
  108. break;
  109. }
  110. }
  111. static inline void spi_enable_chip(struct ft_spi *fts, int enable)
  112. {
  113. ft_writel(fts, SSIENR, (enable ? 1 : 0));
  114. }
  115. static inline void spi_set_clk(struct ft_spi *fts, u16 div)
  116. {
  117. ft_writel(fts, BAUDR, div);
  118. }
  119. static inline void spi_mask_intr(struct ft_spi *fts, u32 mask)
  120. {
  121. u32 new_mask;
  122. new_mask = ft_readl(fts, IMR) & ~mask;
  123. ft_writel(fts, IMR, new_mask);
  124. }
  125. static inline void spi_umask_intr(struct ft_spi *fts, u32 mask)
  126. {
  127. u32 new_mask;
  128. new_mask = ft_readl(fts, IMR) | mask;
  129. ft_writel(fts, IMR, new_mask);
  130. }
  131. static inline void spi_reset_chip(struct ft_spi *fts)
  132. {
  133. spi_enable_chip(fts, 0);
  134. spi_mask_intr(fts, 0xff);
  135. spi_enable_chip(fts, 1);
  136. }
  137. static inline void spi_shutdown_chip(struct ft_spi *fts)
  138. {
  139. spi_enable_chip(fts, 0);
  140. spi_set_clk(fts, 0);
  141. }
  142. struct ft_spi_chip {
  143. u8 poll_mode;
  144. u8 type;
  145. void (*cs_control)(u32 command);
  146. };
  147. struct chip_data {
  148. u8 cs;
  149. u8 tmode;
  150. u8 type;
  151. u8 poll_mode;
  152. u16 clk_div;
  153. u32 speed_hz;
  154. void (*cs_control)(u32 command);
  155. };
  156. static void ft_spi_set_cs(struct spi_device *spi, bool enable)
  157. {
  158. struct ft_spi *fts = spi_master_get_devdata(spi->master);
  159. struct chip_data *chip = spi_get_ctldata(spi);
  160. if (chip && chip->cs_control)
  161. chip->cs_control(!enable);
  162. if (!enable)
  163. ft_writel(fts, SER, BIT(spi->chip_select));
  164. }
  165. static inline u32 tx_max(struct ft_spi *fts)
  166. {
  167. u32 tx_left, tx_room, rxtx_gap;
  168. tx_left = (fts->tx_end - fts->tx) / fts->n_bytes;
  169. tx_room = fts->fifo_len - ft_readl(fts, TXFLR);
  170. rxtx_gap = ((fts->rx_end - fts->rx) - (fts->tx_end - fts->tx))
  171. / fts->n_bytes;
  172. return min3(tx_left, tx_room, (u32) (fts->fifo_len - rxtx_gap));
  173. }
  174. static inline u32 rx_max(struct ft_spi *fts)
  175. {
  176. u32 rx_left = (fts->rx_end - fts->rx) / fts->n_bytes;
  177. return min_t(u32, rx_left, ft_readl(fts, RXFLR));
  178. }
  179. static void ft_writer(struct ft_spi *fts)
  180. {
  181. u32 max = tx_max(fts);
  182. u16 txw = 0;
  183. while (max--) {
  184. if (fts->tx_end - fts->len) {
  185. if (fts->n_bytes == 1)
  186. txw = *(u8 *)(fts->tx);
  187. else
  188. txw = *(u16 *)(fts->tx);
  189. }
  190. ft_write_io_reg(fts, DR, txw);
  191. fts->tx += fts->n_bytes;
  192. }
  193. }
  194. static void ft_reader(struct ft_spi *fts)
  195. {
  196. u32 max = rx_max(fts);
  197. u16 rxw;
  198. while (max--) {
  199. rxw = ft_read_io_reg(fts, DR);
  200. if (fts->rx_end - fts->len) {
  201. if (fts->n_bytes == 1)
  202. *(u8 *)(fts->rx) = rxw;
  203. else
  204. *(u16 *)(fts->rx) = rxw;
  205. }
  206. fts->rx += fts->n_bytes;
  207. }
  208. }
  209. static void int_error_stop(struct ft_spi *fts, const char *msg)
  210. {
  211. spi_reset_chip(fts);
  212. dev_err(&fts->master->dev, "%s\n", msg);
  213. fts->master->cur_msg->status = -EIO;
  214. spi_finalize_current_transfer(fts->master);
  215. }
  216. static irqreturn_t interrupt_transfer(struct ft_spi *fts)
  217. {
  218. u16 irq_status = ft_readl(fts, ISR);
  219. if (irq_status & (INT_TXOI | INT_RXOI | INT_RXUI)) {
  220. ft_readl(fts, ICR);
  221. int_error_stop(fts, "interrupt_transfer: fifo overrun/underrun");
  222. return IRQ_HANDLED;
  223. }
  224. ft_reader(fts);
  225. if (fts->rx_end == fts->rx) {
  226. spi_mask_intr(fts, INT_TXEI);
  227. spi_finalize_current_transfer(fts->master);
  228. return IRQ_HANDLED;
  229. }
  230. if (irq_status & INT_TXEI) {
  231. spi_mask_intr(fts, INT_TXEI);
  232. ft_writer(fts);
  233. spi_umask_intr(fts, INT_TXEI);
  234. }
  235. return IRQ_HANDLED;
  236. }
  237. static irqreturn_t ft_spi_irq(int irq, void *dev_id)
  238. {
  239. struct spi_master *master = dev_id;
  240. struct ft_spi *fts = spi_master_get_devdata(master);
  241. u16 irq_status = ft_readl(fts, ISR) & 0x3f;
  242. if (!irq_status)
  243. return IRQ_NONE;
  244. if (!master->cur_msg) {
  245. spi_mask_intr(fts, INT_TXEI);
  246. return IRQ_HANDLED;
  247. }
  248. return fts->transfer_handler(fts);
  249. }
  250. static int poll_transfer(struct ft_spi *fts)
  251. {
  252. do {
  253. ft_writer(fts);
  254. ft_reader(fts);
  255. cpu_relax();
  256. } while (fts->rx_end > fts->rx);
  257. return 0;
  258. }
  259. static int ft_spi_transfer_one(struct spi_master *master,
  260. struct spi_device *spi, struct spi_transfer *transfer)
  261. {
  262. struct ft_spi *fts = spi_master_get_devdata(master);
  263. struct chip_data *chip = spi_get_ctldata(spi);
  264. u8 imask = 0;
  265. u16 txlevel = 0;
  266. u16 clk_div;
  267. u32 cr0;
  268. fts->tx = (void *)transfer->tx_buf;
  269. fts->tx_end = fts->tx + transfer->len;
  270. fts->rx = transfer->rx_buf;
  271. fts->rx_end = fts->rx + transfer->len;
  272. fts->len = transfer->len;
  273. spi_enable_chip(fts, 0);
  274. if (transfer->speed_hz != chip->speed_hz) {
  275. clk_div = (fts->max_freq / transfer->speed_hz + 1) & 0xfffe;
  276. chip->speed_hz = transfer->speed_hz;
  277. chip->clk_div = clk_div;
  278. spi_set_clk(fts, chip->clk_div);
  279. }
  280. if (transfer->bits_per_word == 8) {
  281. fts->n_bytes = 1;
  282. } else if (transfer->bits_per_word == 16) {
  283. fts->n_bytes = 2;
  284. } else {
  285. return -EINVAL;
  286. }
  287. cr0 = (transfer->bits_per_word - 1)
  288. | (chip->type << FRF_OFFSET)
  289. | (spi->mode << MODE_OFFSET)
  290. | (chip->tmode << TMOD_OFFSET);
  291. if (chip->cs_control) {
  292. if (fts->rx && fts->tx)
  293. chip->tmode = TMOD_TR;
  294. else if (fts->rx)
  295. chip->tmode = TMOD_RO;
  296. else
  297. chip->tmode = TMOD_TO;
  298. cr0 &= ~TMOD_MASK;
  299. cr0 |= (chip->tmode << TMOD_OFFSET);
  300. }
  301. ft_writel(fts, CTRL0, cr0);
  302. spi_mask_intr(fts, 0xff);
  303. if (!chip->poll_mode) {
  304. txlevel = min_t(u16, fts->fifo_len / 2, fts->len / fts->n_bytes);
  305. ft_writel(fts, TXFLTR, txlevel);
  306. imask |= INT_TXEI | INT_TXOI |
  307. INT_RXUI | INT_RXOI;
  308. spi_umask_intr(fts, imask);
  309. fts->transfer_handler = interrupt_transfer;
  310. }
  311. spi_enable_chip(fts, 1);
  312. if (chip->poll_mode)
  313. return poll_transfer(fts);
  314. return 1;
  315. }
  316. static void ft_spi_handle_err(struct spi_master *master,
  317. struct spi_message *msg)
  318. {
  319. struct ft_spi *fts = spi_master_get_devdata(master);
  320. spi_reset_chip(fts);
  321. }
  322. static int ft_spi_setup(struct spi_device *spi)
  323. {
  324. struct ft_spi_chip *chip_info = NULL;
  325. struct chip_data *chip;
  326. int ret;
  327. chip = spi_get_ctldata(spi);
  328. if (!chip) {
  329. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  330. if (!chip)
  331. return -ENOMEM;
  332. spi_set_ctldata(spi, chip);
  333. }
  334. chip_info = spi->controller_data;
  335. if (chip_info) {
  336. if (chip_info->cs_control)
  337. chip->cs_control = chip_info->cs_control;
  338. chip->poll_mode = chip_info->poll_mode;
  339. chip->type = chip_info->type;
  340. }
  341. chip->tmode = 0;
  342. if (gpio_is_valid(spi->cs_gpio)) {
  343. ret = gpio_direction_output(spi->cs_gpio,
  344. !(spi->mode & SPI_CS_HIGH));
  345. if (ret)
  346. return ret;
  347. }
  348. return 0;
  349. }
  350. static void ft_spi_cleanup(struct spi_device *spi)
  351. {
  352. struct chip_data *chip = spi_get_ctldata(spi);
  353. kfree(chip);
  354. spi_set_ctldata(spi, NULL);
  355. }
  356. static void spi_hw_init(struct device *dev, struct ft_spi *fts)
  357. {
  358. spi_reset_chip(fts);
  359. if (!fts->fifo_len) {
  360. u32 fifo;
  361. for (fifo = 1; fifo < 256; fifo++) {
  362. ft_writel(fts, TXFLTR, fifo);
  363. if (fifo != ft_readl(fts, TXFLTR))
  364. break;
  365. }
  366. ft_writel(fts, TXFLTR, 0);
  367. fts->fifo_len = (fifo == 1) ? 0 : fifo;
  368. dev_dbg(dev, "Detected FIFO size: %u bytes\n", fts->fifo_len);
  369. }
  370. }
  371. int ft_spi_add_host(struct device *dev, struct ft_spi *fts)
  372. {
  373. struct spi_master *master;
  374. int ret;
  375. BUG_ON(fts == NULL);
  376. master = spi_alloc_master(dev, 0);
  377. if (!master)
  378. return -ENOMEM;
  379. fts->master = master;
  380. snprintf(fts->name, sizeof(fts->name), "ft_spi%d", fts->bus_num);
  381. ret = request_irq(fts->irq, ft_spi_irq, IRQF_SHARED, fts->name, master);
  382. if (ret < 0) {
  383. dev_err(dev, "can not get IRQ\n");
  384. goto err_free_master;
  385. }
  386. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
  387. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  388. master->bus_num = fts->bus_num;
  389. master->num_chipselect = fts->num_cs;
  390. master->setup = ft_spi_setup;
  391. master->cleanup = ft_spi_cleanup;
  392. master->set_cs = ft_spi_set_cs;
  393. master->transfer_one = ft_spi_transfer_one;
  394. master->handle_err = ft_spi_handle_err;
  395. master->max_speed_hz = fts->max_freq;
  396. master->dev.of_node = dev->of_node;
  397. spi_hw_init(dev, fts);
  398. spi_master_set_devdata(master, fts);
  399. ret = devm_spi_register_master(dev, master);
  400. if (ret) {
  401. dev_err(&master->dev, "problem registering spi master\n");
  402. goto err_exit;
  403. }
  404. return 0;
  405. err_exit:
  406. spi_enable_chip(fts, 0);
  407. free_irq(fts->irq, master);
  408. err_free_master:
  409. spi_master_put(master);
  410. return ret;
  411. }
  412. void ft_spi_remove_host(struct ft_spi *fts)
  413. {
  414. spi_shutdown_chip(fts);
  415. free_irq(fts->irq, fts->master);
  416. }
  417. #define DRIVER_NAME "phytium_spi"
  418. struct ft_spi_clk {
  419. struct ft_spi fts;
  420. struct clk *clk;
  421. };
  422. static int ft_spi_probe(struct platform_device *pdev)
  423. {
  424. struct ft_spi_clk *ftsc;
  425. struct ft_spi *fts;
  426. struct resource *mem;
  427. int ret;
  428. int num_cs;
  429. ftsc = devm_kzalloc(&pdev->dev, sizeof(struct ft_spi_clk),
  430. GFP_KERNEL);
  431. if (!ftsc)
  432. return -ENOMEM;
  433. fts = &ftsc->fts;
  434. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  435. if (!mem) {
  436. dev_err(&pdev->dev, "no mem resource?\n");
  437. return -EINVAL;
  438. }
  439. fts->regs = devm_ioremap_resource(&pdev->dev, mem);
  440. if (IS_ERR(fts->regs)) {
  441. dev_err(&pdev->dev, "SPI region map failed\n");
  442. return PTR_ERR(fts->regs);
  443. }
  444. fts->irq = platform_get_irq(pdev, 0);
  445. if (fts->irq < 0) {
  446. dev_err(&pdev->dev, "no irq resource?\n");
  447. return fts->irq; /* -ENXIO */
  448. }
  449. if (pdev->dev.of_node) {
  450. ftsc->clk = devm_clk_get(&pdev->dev, NULL);
  451. if (IS_ERR(ftsc->clk))
  452. return PTR_ERR(ftsc->clk);
  453. ret = clk_prepare_enable(ftsc->clk);
  454. if (ret)
  455. return ret;
  456. fts->max_freq = clk_get_rate(ftsc->clk);
  457. } else if (has_acpi_companion(&pdev->dev)) {
  458. fts->max_freq = 48000000;
  459. }
  460. fts->bus_num = pdev->id;
  461. device_property_read_u32(&pdev->dev, "reg-io-width", &fts->reg_io_width);
  462. num_cs = 4;
  463. device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
  464. fts->num_cs = num_cs;
  465. if (pdev->dev.of_node) {
  466. int i;
  467. for (i = 0; i < fts->num_cs; i++) {
  468. int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
  469. "cs-gpios", i);
  470. if (cs_gpio == -EPROBE_DEFER) {
  471. ret = cs_gpio;
  472. goto out;
  473. }
  474. if (gpio_is_valid(cs_gpio)) {
  475. ret = devm_gpio_request(&pdev->dev, cs_gpio,
  476. dev_name(&pdev->dev));
  477. if (ret)
  478. goto out;
  479. }
  480. }
  481. }
  482. ret = ft_spi_add_host(&pdev->dev, fts);
  483. if (ret)
  484. goto out;
  485. platform_set_drvdata(pdev, ftsc);
  486. return 0;
  487. out:
  488. clk_disable_unprepare(ftsc->clk);
  489. return ret;
  490. }
  491. static int ft_spi_remove(struct platform_device *pdev)
  492. {
  493. struct ft_spi_clk *ftsc = platform_get_drvdata(pdev);
  494. ft_spi_remove_host(&ftsc->fts);
  495. clk_disable_unprepare(ftsc->clk);
  496. return 0;
  497. }
  498. static const struct of_device_id ft_spi_of_match[] = {
  499. { .compatible = "phytium,spi", },
  500. { /* end of table */}
  501. };
  502. MODULE_DEVICE_TABLE(of, ft_spi_of_match);
  503. static const struct acpi_device_id ft_spi_acpi_match[] = {
  504. {"PHTY000E", 0},
  505. {}
  506. };
  507. MODULE_DEVICE_TABLE(acpi, ft_spi_acpi_match);
  508. static struct platform_driver ft_spi_driver = {
  509. .probe = ft_spi_probe,
  510. .remove = ft_spi_remove,
  511. .driver = {
  512. .name = DRIVER_NAME,
  513. .of_match_table = of_match_ptr(ft_spi_of_match),
  514. .acpi_match_table = ACPI_PTR(ft_spi_acpi_match),
  515. },
  516. };
  517. module_platform_driver(ft_spi_driver);
  518. MODULE_AUTHOR("Mingshuai Zhu <zhumingshuai@phytium.com.cn>");
  519. MODULE_AUTHOR("Chen Baozi <chenbaozi@phytium.com.cn>");
  520. MODULE_DESCRIPTION("Driver for Phytium SPI controller core");
  521. MODULE_LICENSE("GPL v2");