spi-tegra114.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * SPI driver for NVIDIA's Tegra114 SPI Controller.
  3. *
  4. * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
  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. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/dmapool.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/kernel.h>
  28. #include <linux/kthread.h>
  29. #include <linux/module.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/reset.h>
  35. #include <linux/spi/spi.h>
  36. #define SPI_COMMAND1 0x000
  37. #define SPI_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  38. #define SPI_PACKED (1 << 5)
  39. #define SPI_TX_EN (1 << 11)
  40. #define SPI_RX_EN (1 << 12)
  41. #define SPI_BOTH_EN_BYTE (1 << 13)
  42. #define SPI_BOTH_EN_BIT (1 << 14)
  43. #define SPI_LSBYTE_FE (1 << 15)
  44. #define SPI_LSBIT_FE (1 << 16)
  45. #define SPI_BIDIROE (1 << 17)
  46. #define SPI_IDLE_SDA_DRIVE_LOW (0 << 18)
  47. #define SPI_IDLE_SDA_DRIVE_HIGH (1 << 18)
  48. #define SPI_IDLE_SDA_PULL_LOW (2 << 18)
  49. #define SPI_IDLE_SDA_PULL_HIGH (3 << 18)
  50. #define SPI_IDLE_SDA_MASK (3 << 18)
  51. #define SPI_CS_SS_VAL (1 << 20)
  52. #define SPI_CS_SW_HW (1 << 21)
  53. /* SPI_CS_POL_INACTIVE bits are default high */
  54. /* n from 0 to 3 */
  55. #define SPI_CS_POL_INACTIVE(n) (1 << (22 + (n)))
  56. #define SPI_CS_POL_INACTIVE_MASK (0xF << 22)
  57. #define SPI_CS_SEL_0 (0 << 26)
  58. #define SPI_CS_SEL_1 (1 << 26)
  59. #define SPI_CS_SEL_2 (2 << 26)
  60. #define SPI_CS_SEL_3 (3 << 26)
  61. #define SPI_CS_SEL_MASK (3 << 26)
  62. #define SPI_CS_SEL(x) (((x) & 0x3) << 26)
  63. #define SPI_CONTROL_MODE_0 (0 << 28)
  64. #define SPI_CONTROL_MODE_1 (1 << 28)
  65. #define SPI_CONTROL_MODE_2 (2 << 28)
  66. #define SPI_CONTROL_MODE_3 (3 << 28)
  67. #define SPI_CONTROL_MODE_MASK (3 << 28)
  68. #define SPI_MODE_SEL(x) (((x) & 0x3) << 28)
  69. #define SPI_M_S (1 << 30)
  70. #define SPI_PIO (1 << 31)
  71. #define SPI_COMMAND2 0x004
  72. #define SPI_TX_TAP_DELAY(x) (((x) & 0x3F) << 6)
  73. #define SPI_RX_TAP_DELAY(x) (((x) & 0x3F) << 0)
  74. #define SPI_CS_TIMING1 0x008
  75. #define SPI_SETUP_HOLD(setup, hold) (((setup) << 4) | (hold))
  76. #define SPI_CS_SETUP_HOLD(reg, cs, val) \
  77. ((((val) & 0xFFu) << ((cs) * 8)) | \
  78. ((reg) & ~(0xFFu << ((cs) * 8))))
  79. #define SPI_CS_TIMING2 0x00C
  80. #define CYCLES_BETWEEN_PACKETS_0(x) (((x) & 0x1F) << 0)
  81. #define CS_ACTIVE_BETWEEN_PACKETS_0 (1 << 5)
  82. #define CYCLES_BETWEEN_PACKETS_1(x) (((x) & 0x1F) << 8)
  83. #define CS_ACTIVE_BETWEEN_PACKETS_1 (1 << 13)
  84. #define CYCLES_BETWEEN_PACKETS_2(x) (((x) & 0x1F) << 16)
  85. #define CS_ACTIVE_BETWEEN_PACKETS_2 (1 << 21)
  86. #define CYCLES_BETWEEN_PACKETS_3(x) (((x) & 0x1F) << 24)
  87. #define CS_ACTIVE_BETWEEN_PACKETS_3 (1 << 29)
  88. #define SPI_SET_CS_ACTIVE_BETWEEN_PACKETS(reg, cs, val) \
  89. (reg = (((val) & 0x1) << ((cs) * 8 + 5)) | \
  90. ((reg) & ~(1 << ((cs) * 8 + 5))))
  91. #define SPI_SET_CYCLES_BETWEEN_PACKETS(reg, cs, val) \
  92. (reg = (((val) & 0xF) << ((cs) * 8)) | \
  93. ((reg) & ~(0xF << ((cs) * 8))))
  94. #define SPI_TRANS_STATUS 0x010
  95. #define SPI_BLK_CNT(val) (((val) >> 0) & 0xFFFF)
  96. #define SPI_SLV_IDLE_COUNT(val) (((val) >> 16) & 0xFF)
  97. #define SPI_RDY (1 << 30)
  98. #define SPI_FIFO_STATUS 0x014
  99. #define SPI_RX_FIFO_EMPTY (1 << 0)
  100. #define SPI_RX_FIFO_FULL (1 << 1)
  101. #define SPI_TX_FIFO_EMPTY (1 << 2)
  102. #define SPI_TX_FIFO_FULL (1 << 3)
  103. #define SPI_RX_FIFO_UNF (1 << 4)
  104. #define SPI_RX_FIFO_OVF (1 << 5)
  105. #define SPI_TX_FIFO_UNF (1 << 6)
  106. #define SPI_TX_FIFO_OVF (1 << 7)
  107. #define SPI_ERR (1 << 8)
  108. #define SPI_TX_FIFO_FLUSH (1 << 14)
  109. #define SPI_RX_FIFO_FLUSH (1 << 15)
  110. #define SPI_TX_FIFO_EMPTY_COUNT(val) (((val) >> 16) & 0x7F)
  111. #define SPI_RX_FIFO_FULL_COUNT(val) (((val) >> 23) & 0x7F)
  112. #define SPI_FRAME_END (1 << 30)
  113. #define SPI_CS_INACTIVE (1 << 31)
  114. #define SPI_FIFO_ERROR (SPI_RX_FIFO_UNF | \
  115. SPI_RX_FIFO_OVF | SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF)
  116. #define SPI_FIFO_EMPTY (SPI_RX_FIFO_EMPTY | SPI_TX_FIFO_EMPTY)
  117. #define SPI_TX_DATA 0x018
  118. #define SPI_RX_DATA 0x01C
  119. #define SPI_DMA_CTL 0x020
  120. #define SPI_TX_TRIG_1 (0 << 15)
  121. #define SPI_TX_TRIG_4 (1 << 15)
  122. #define SPI_TX_TRIG_8 (2 << 15)
  123. #define SPI_TX_TRIG_16 (3 << 15)
  124. #define SPI_TX_TRIG_MASK (3 << 15)
  125. #define SPI_RX_TRIG_1 (0 << 19)
  126. #define SPI_RX_TRIG_4 (1 << 19)
  127. #define SPI_RX_TRIG_8 (2 << 19)
  128. #define SPI_RX_TRIG_16 (3 << 19)
  129. #define SPI_RX_TRIG_MASK (3 << 19)
  130. #define SPI_IE_TX (1 << 28)
  131. #define SPI_IE_RX (1 << 29)
  132. #define SPI_CONT (1 << 30)
  133. #define SPI_DMA (1 << 31)
  134. #define SPI_DMA_EN SPI_DMA
  135. #define SPI_DMA_BLK 0x024
  136. #define SPI_DMA_BLK_SET(x) (((x) & 0xFFFF) << 0)
  137. #define SPI_TX_FIFO 0x108
  138. #define SPI_RX_FIFO 0x188
  139. #define MAX_CHIP_SELECT 4
  140. #define SPI_FIFO_DEPTH 64
  141. #define DATA_DIR_TX (1 << 0)
  142. #define DATA_DIR_RX (1 << 1)
  143. #define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
  144. #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
  145. #define TX_FIFO_EMPTY_COUNT_MAX SPI_TX_FIFO_EMPTY_COUNT(0x40)
  146. #define RX_FIFO_FULL_COUNT_ZERO SPI_RX_FIFO_FULL_COUNT(0)
  147. #define MAX_HOLD_CYCLES 16
  148. #define SPI_DEFAULT_SPEED 25000000
  149. struct tegra_spi_data {
  150. struct device *dev;
  151. struct spi_master *master;
  152. spinlock_t lock;
  153. struct clk *clk;
  154. struct reset_control *rst;
  155. void __iomem *base;
  156. phys_addr_t phys;
  157. unsigned irq;
  158. u32 cur_speed;
  159. struct spi_device *cur_spi;
  160. struct spi_device *cs_control;
  161. unsigned cur_pos;
  162. unsigned words_per_32bit;
  163. unsigned bytes_per_word;
  164. unsigned curr_dma_words;
  165. unsigned cur_direction;
  166. unsigned cur_rx_pos;
  167. unsigned cur_tx_pos;
  168. unsigned dma_buf_size;
  169. unsigned max_buf_size;
  170. bool is_curr_dma_xfer;
  171. struct completion rx_dma_complete;
  172. struct completion tx_dma_complete;
  173. u32 tx_status;
  174. u32 rx_status;
  175. u32 status_reg;
  176. bool is_packed;
  177. u32 command1_reg;
  178. u32 dma_control_reg;
  179. u32 def_command1_reg;
  180. struct completion xfer_completion;
  181. struct spi_transfer *curr_xfer;
  182. struct dma_chan *rx_dma_chan;
  183. u32 *rx_dma_buf;
  184. dma_addr_t rx_dma_phys;
  185. struct dma_async_tx_descriptor *rx_dma_desc;
  186. struct dma_chan *tx_dma_chan;
  187. u32 *tx_dma_buf;
  188. dma_addr_t tx_dma_phys;
  189. struct dma_async_tx_descriptor *tx_dma_desc;
  190. };
  191. static int tegra_spi_runtime_suspend(struct device *dev);
  192. static int tegra_spi_runtime_resume(struct device *dev);
  193. static inline u32 tegra_spi_readl(struct tegra_spi_data *tspi,
  194. unsigned long reg)
  195. {
  196. return readl(tspi->base + reg);
  197. }
  198. static inline void tegra_spi_writel(struct tegra_spi_data *tspi,
  199. u32 val, unsigned long reg)
  200. {
  201. writel(val, tspi->base + reg);
  202. /* Read back register to make sure that register writes completed */
  203. if (reg != SPI_TX_FIFO)
  204. readl(tspi->base + SPI_COMMAND1);
  205. }
  206. static void tegra_spi_clear_status(struct tegra_spi_data *tspi)
  207. {
  208. u32 val;
  209. /* Write 1 to clear status register */
  210. val = tegra_spi_readl(tspi, SPI_TRANS_STATUS);
  211. tegra_spi_writel(tspi, val, SPI_TRANS_STATUS);
  212. /* Clear fifo status error if any */
  213. val = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  214. if (val & SPI_ERR)
  215. tegra_spi_writel(tspi, SPI_ERR | SPI_FIFO_ERROR,
  216. SPI_FIFO_STATUS);
  217. }
  218. static unsigned tegra_spi_calculate_curr_xfer_param(
  219. struct spi_device *spi, struct tegra_spi_data *tspi,
  220. struct spi_transfer *t)
  221. {
  222. unsigned remain_len = t->len - tspi->cur_pos;
  223. unsigned max_word;
  224. unsigned bits_per_word = t->bits_per_word;
  225. unsigned max_len;
  226. unsigned total_fifo_words;
  227. tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
  228. if (bits_per_word == 8 || bits_per_word == 16) {
  229. tspi->is_packed = 1;
  230. tspi->words_per_32bit = 32/bits_per_word;
  231. } else {
  232. tspi->is_packed = 0;
  233. tspi->words_per_32bit = 1;
  234. }
  235. if (tspi->is_packed) {
  236. max_len = min(remain_len, tspi->max_buf_size);
  237. tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  238. total_fifo_words = (max_len + 3) / 4;
  239. } else {
  240. max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  241. max_word = min(max_word, tspi->max_buf_size/4);
  242. tspi->curr_dma_words = max_word;
  243. total_fifo_words = max_word;
  244. }
  245. return total_fifo_words;
  246. }
  247. static unsigned tegra_spi_fill_tx_fifo_from_client_txbuf(
  248. struct tegra_spi_data *tspi, struct spi_transfer *t)
  249. {
  250. unsigned nbytes;
  251. unsigned tx_empty_count;
  252. u32 fifo_status;
  253. unsigned max_n_32bit;
  254. unsigned i, count;
  255. unsigned int written_words;
  256. unsigned fifo_words_left;
  257. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  258. fifo_status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  259. tx_empty_count = SPI_TX_FIFO_EMPTY_COUNT(fifo_status);
  260. if (tspi->is_packed) {
  261. fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  262. written_words = min(fifo_words_left, tspi->curr_dma_words);
  263. nbytes = written_words * tspi->bytes_per_word;
  264. max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  265. for (count = 0; count < max_n_32bit; count++) {
  266. u32 x = 0;
  267. for (i = 0; (i < 4) && nbytes; i++, nbytes--)
  268. x |= (u32)(*tx_buf++) << (i * 8);
  269. tegra_spi_writel(tspi, x, SPI_TX_FIFO);
  270. }
  271. } else {
  272. max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
  273. written_words = max_n_32bit;
  274. nbytes = written_words * tspi->bytes_per_word;
  275. for (count = 0; count < max_n_32bit; count++) {
  276. u32 x = 0;
  277. for (i = 0; nbytes && (i < tspi->bytes_per_word);
  278. i++, nbytes--)
  279. x |= (u32)(*tx_buf++) << (i * 8);
  280. tegra_spi_writel(tspi, x, SPI_TX_FIFO);
  281. }
  282. }
  283. tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  284. return written_words;
  285. }
  286. static unsigned int tegra_spi_read_rx_fifo_to_client_rxbuf(
  287. struct tegra_spi_data *tspi, struct spi_transfer *t)
  288. {
  289. unsigned rx_full_count;
  290. u32 fifo_status;
  291. unsigned i, count;
  292. unsigned int read_words = 0;
  293. unsigned len;
  294. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  295. fifo_status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  296. rx_full_count = SPI_RX_FIFO_FULL_COUNT(fifo_status);
  297. if (tspi->is_packed) {
  298. len = tspi->curr_dma_words * tspi->bytes_per_word;
  299. for (count = 0; count < rx_full_count; count++) {
  300. u32 x = tegra_spi_readl(tspi, SPI_RX_FIFO);
  301. for (i = 0; len && (i < 4); i++, len--)
  302. *rx_buf++ = (x >> i*8) & 0xFF;
  303. }
  304. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  305. read_words += tspi->curr_dma_words;
  306. } else {
  307. u32 rx_mask = ((u32)1 << t->bits_per_word) - 1;
  308. for (count = 0; count < rx_full_count; count++) {
  309. u32 x = tegra_spi_readl(tspi, SPI_RX_FIFO) & rx_mask;
  310. for (i = 0; (i < tspi->bytes_per_word); i++)
  311. *rx_buf++ = (x >> (i*8)) & 0xFF;
  312. }
  313. tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
  314. read_words += rx_full_count;
  315. }
  316. return read_words;
  317. }
  318. static void tegra_spi_copy_client_txbuf_to_spi_txbuf(
  319. struct tegra_spi_data *tspi, struct spi_transfer *t)
  320. {
  321. /* Make the dma buffer to read by cpu */
  322. dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  323. tspi->dma_buf_size, DMA_TO_DEVICE);
  324. if (tspi->is_packed) {
  325. unsigned len = tspi->curr_dma_words * tspi->bytes_per_word;
  326. memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  327. } else {
  328. unsigned int i;
  329. unsigned int count;
  330. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  331. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  332. for (count = 0; count < tspi->curr_dma_words; count++) {
  333. u32 x = 0;
  334. for (i = 0; consume && (i < tspi->bytes_per_word);
  335. i++, consume--)
  336. x |= (u32)(*tx_buf++) << (i * 8);
  337. tspi->tx_dma_buf[count] = x;
  338. }
  339. }
  340. tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  341. /* Make the dma buffer to read by dma */
  342. dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  343. tspi->dma_buf_size, DMA_TO_DEVICE);
  344. }
  345. static void tegra_spi_copy_spi_rxbuf_to_client_rxbuf(
  346. struct tegra_spi_data *tspi, struct spi_transfer *t)
  347. {
  348. /* Make the dma buffer to read by cpu */
  349. dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  350. tspi->dma_buf_size, DMA_FROM_DEVICE);
  351. if (tspi->is_packed) {
  352. unsigned len = tspi->curr_dma_words * tspi->bytes_per_word;
  353. memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  354. } else {
  355. unsigned int i;
  356. unsigned int count;
  357. unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
  358. u32 rx_mask = ((u32)1 << t->bits_per_word) - 1;
  359. for (count = 0; count < tspi->curr_dma_words; count++) {
  360. u32 x = tspi->rx_dma_buf[count] & rx_mask;
  361. for (i = 0; (i < tspi->bytes_per_word); i++)
  362. *rx_buf++ = (x >> (i*8)) & 0xFF;
  363. }
  364. }
  365. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  366. /* Make the dma buffer to read by dma */
  367. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  368. tspi->dma_buf_size, DMA_FROM_DEVICE);
  369. }
  370. static void tegra_spi_dma_complete(void *args)
  371. {
  372. struct completion *dma_complete = args;
  373. complete(dma_complete);
  374. }
  375. static int tegra_spi_start_tx_dma(struct tegra_spi_data *tspi, int len)
  376. {
  377. reinit_completion(&tspi->tx_dma_complete);
  378. tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  379. tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
  380. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  381. if (!tspi->tx_dma_desc) {
  382. dev_err(tspi->dev, "Not able to get desc for Tx\n");
  383. return -EIO;
  384. }
  385. tspi->tx_dma_desc->callback = tegra_spi_dma_complete;
  386. tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  387. dmaengine_submit(tspi->tx_dma_desc);
  388. dma_async_issue_pending(tspi->tx_dma_chan);
  389. return 0;
  390. }
  391. static int tegra_spi_start_rx_dma(struct tegra_spi_data *tspi, int len)
  392. {
  393. reinit_completion(&tspi->rx_dma_complete);
  394. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  395. tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
  396. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  397. if (!tspi->rx_dma_desc) {
  398. dev_err(tspi->dev, "Not able to get desc for Rx\n");
  399. return -EIO;
  400. }
  401. tspi->rx_dma_desc->callback = tegra_spi_dma_complete;
  402. tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  403. dmaengine_submit(tspi->rx_dma_desc);
  404. dma_async_issue_pending(tspi->rx_dma_chan);
  405. return 0;
  406. }
  407. static int tegra_spi_start_dma_based_transfer(
  408. struct tegra_spi_data *tspi, struct spi_transfer *t)
  409. {
  410. u32 val;
  411. unsigned int len;
  412. int ret = 0;
  413. u32 status;
  414. /* Make sure that Rx and Tx fifo are empty */
  415. status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  416. if ((status & SPI_FIFO_EMPTY) != SPI_FIFO_EMPTY) {
  417. dev_err(tspi->dev, "Rx/Tx fifo are not empty status 0x%08x\n",
  418. (unsigned)status);
  419. return -EIO;
  420. }
  421. val = SPI_DMA_BLK_SET(tspi->curr_dma_words - 1);
  422. tegra_spi_writel(tspi, val, SPI_DMA_BLK);
  423. if (tspi->is_packed)
  424. len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  425. 4) * 4;
  426. else
  427. len = tspi->curr_dma_words * 4;
  428. /* Set attention level based on length of transfer */
  429. if (len & 0xF)
  430. val |= SPI_TX_TRIG_1 | SPI_RX_TRIG_1;
  431. else if (((len) >> 4) & 0x1)
  432. val |= SPI_TX_TRIG_4 | SPI_RX_TRIG_4;
  433. else
  434. val |= SPI_TX_TRIG_8 | SPI_RX_TRIG_8;
  435. if (tspi->cur_direction & DATA_DIR_TX)
  436. val |= SPI_IE_TX;
  437. if (tspi->cur_direction & DATA_DIR_RX)
  438. val |= SPI_IE_RX;
  439. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  440. tspi->dma_control_reg = val;
  441. if (tspi->cur_direction & DATA_DIR_TX) {
  442. tegra_spi_copy_client_txbuf_to_spi_txbuf(tspi, t);
  443. ret = tegra_spi_start_tx_dma(tspi, len);
  444. if (ret < 0) {
  445. dev_err(tspi->dev,
  446. "Starting tx dma failed, err %d\n", ret);
  447. return ret;
  448. }
  449. }
  450. if (tspi->cur_direction & DATA_DIR_RX) {
  451. /* Make the dma buffer to read by dma */
  452. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  453. tspi->dma_buf_size, DMA_FROM_DEVICE);
  454. ret = tegra_spi_start_rx_dma(tspi, len);
  455. if (ret < 0) {
  456. dev_err(tspi->dev,
  457. "Starting rx dma failed, err %d\n", ret);
  458. if (tspi->cur_direction & DATA_DIR_TX)
  459. dmaengine_terminate_all(tspi->tx_dma_chan);
  460. return ret;
  461. }
  462. }
  463. tspi->is_curr_dma_xfer = true;
  464. tspi->dma_control_reg = val;
  465. val |= SPI_DMA_EN;
  466. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  467. return ret;
  468. }
  469. static int tegra_spi_start_cpu_based_transfer(
  470. struct tegra_spi_data *tspi, struct spi_transfer *t)
  471. {
  472. u32 val;
  473. unsigned cur_words;
  474. if (tspi->cur_direction & DATA_DIR_TX)
  475. cur_words = tegra_spi_fill_tx_fifo_from_client_txbuf(tspi, t);
  476. else
  477. cur_words = tspi->curr_dma_words;
  478. val = SPI_DMA_BLK_SET(cur_words - 1);
  479. tegra_spi_writel(tspi, val, SPI_DMA_BLK);
  480. val = 0;
  481. if (tspi->cur_direction & DATA_DIR_TX)
  482. val |= SPI_IE_TX;
  483. if (tspi->cur_direction & DATA_DIR_RX)
  484. val |= SPI_IE_RX;
  485. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  486. tspi->dma_control_reg = val;
  487. tspi->is_curr_dma_xfer = false;
  488. val |= SPI_DMA_EN;
  489. tegra_spi_writel(tspi, val, SPI_DMA_CTL);
  490. return 0;
  491. }
  492. static int tegra_spi_init_dma_param(struct tegra_spi_data *tspi,
  493. bool dma_to_memory)
  494. {
  495. struct dma_chan *dma_chan;
  496. u32 *dma_buf;
  497. dma_addr_t dma_phys;
  498. int ret;
  499. struct dma_slave_config dma_sconfig;
  500. dma_chan = dma_request_slave_channel_reason(tspi->dev,
  501. dma_to_memory ? "rx" : "tx");
  502. if (IS_ERR(dma_chan)) {
  503. ret = PTR_ERR(dma_chan);
  504. if (ret != -EPROBE_DEFER)
  505. dev_err(tspi->dev,
  506. "Dma channel is not available: %d\n", ret);
  507. return ret;
  508. }
  509. dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  510. &dma_phys, GFP_KERNEL);
  511. if (!dma_buf) {
  512. dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
  513. dma_release_channel(dma_chan);
  514. return -ENOMEM;
  515. }
  516. if (dma_to_memory) {
  517. dma_sconfig.src_addr = tspi->phys + SPI_RX_FIFO;
  518. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  519. dma_sconfig.src_maxburst = 0;
  520. } else {
  521. dma_sconfig.dst_addr = tspi->phys + SPI_TX_FIFO;
  522. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  523. dma_sconfig.dst_maxburst = 0;
  524. }
  525. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  526. if (ret)
  527. goto scrub;
  528. if (dma_to_memory) {
  529. tspi->rx_dma_chan = dma_chan;
  530. tspi->rx_dma_buf = dma_buf;
  531. tspi->rx_dma_phys = dma_phys;
  532. } else {
  533. tspi->tx_dma_chan = dma_chan;
  534. tspi->tx_dma_buf = dma_buf;
  535. tspi->tx_dma_phys = dma_phys;
  536. }
  537. return 0;
  538. scrub:
  539. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  540. dma_release_channel(dma_chan);
  541. return ret;
  542. }
  543. static void tegra_spi_deinit_dma_param(struct tegra_spi_data *tspi,
  544. bool dma_to_memory)
  545. {
  546. u32 *dma_buf;
  547. dma_addr_t dma_phys;
  548. struct dma_chan *dma_chan;
  549. if (dma_to_memory) {
  550. dma_buf = tspi->rx_dma_buf;
  551. dma_chan = tspi->rx_dma_chan;
  552. dma_phys = tspi->rx_dma_phys;
  553. tspi->rx_dma_chan = NULL;
  554. tspi->rx_dma_buf = NULL;
  555. } else {
  556. dma_buf = tspi->tx_dma_buf;
  557. dma_chan = tspi->tx_dma_chan;
  558. dma_phys = tspi->tx_dma_phys;
  559. tspi->tx_dma_buf = NULL;
  560. tspi->tx_dma_chan = NULL;
  561. }
  562. if (!dma_chan)
  563. return;
  564. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  565. dma_release_channel(dma_chan);
  566. }
  567. static u32 tegra_spi_setup_transfer_one(struct spi_device *spi,
  568. struct spi_transfer *t, bool is_first_of_msg)
  569. {
  570. struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
  571. u32 speed = t->speed_hz;
  572. u8 bits_per_word = t->bits_per_word;
  573. u32 command1;
  574. int req_mode;
  575. if (speed != tspi->cur_speed) {
  576. clk_set_rate(tspi->clk, speed);
  577. tspi->cur_speed = speed;
  578. }
  579. tspi->cur_spi = spi;
  580. tspi->cur_pos = 0;
  581. tspi->cur_rx_pos = 0;
  582. tspi->cur_tx_pos = 0;
  583. tspi->curr_xfer = t;
  584. if (is_first_of_msg) {
  585. tegra_spi_clear_status(tspi);
  586. command1 = tspi->def_command1_reg;
  587. command1 |= SPI_BIT_LENGTH(bits_per_word - 1);
  588. command1 &= ~SPI_CONTROL_MODE_MASK;
  589. req_mode = spi->mode & 0x3;
  590. if (req_mode == SPI_MODE_0)
  591. command1 |= SPI_CONTROL_MODE_0;
  592. else if (req_mode == SPI_MODE_1)
  593. command1 |= SPI_CONTROL_MODE_1;
  594. else if (req_mode == SPI_MODE_2)
  595. command1 |= SPI_CONTROL_MODE_2;
  596. else if (req_mode == SPI_MODE_3)
  597. command1 |= SPI_CONTROL_MODE_3;
  598. if (tspi->cs_control) {
  599. if (tspi->cs_control != spi)
  600. tegra_spi_writel(tspi, command1, SPI_COMMAND1);
  601. tspi->cs_control = NULL;
  602. } else
  603. tegra_spi_writel(tspi, command1, SPI_COMMAND1);
  604. command1 |= SPI_CS_SW_HW;
  605. if (spi->mode & SPI_CS_HIGH)
  606. command1 |= SPI_CS_SS_VAL;
  607. else
  608. command1 &= ~SPI_CS_SS_VAL;
  609. tegra_spi_writel(tspi, 0, SPI_COMMAND2);
  610. } else {
  611. command1 = tspi->command1_reg;
  612. command1 &= ~SPI_BIT_LENGTH(~0);
  613. command1 |= SPI_BIT_LENGTH(bits_per_word - 1);
  614. }
  615. return command1;
  616. }
  617. static int tegra_spi_start_transfer_one(struct spi_device *spi,
  618. struct spi_transfer *t, u32 command1)
  619. {
  620. struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
  621. unsigned total_fifo_words;
  622. int ret;
  623. total_fifo_words = tegra_spi_calculate_curr_xfer_param(spi, tspi, t);
  624. if (tspi->is_packed)
  625. command1 |= SPI_PACKED;
  626. command1 &= ~(SPI_CS_SEL_MASK | SPI_TX_EN | SPI_RX_EN);
  627. tspi->cur_direction = 0;
  628. if (t->rx_buf) {
  629. command1 |= SPI_RX_EN;
  630. tspi->cur_direction |= DATA_DIR_RX;
  631. }
  632. if (t->tx_buf) {
  633. command1 |= SPI_TX_EN;
  634. tspi->cur_direction |= DATA_DIR_TX;
  635. }
  636. command1 |= SPI_CS_SEL(spi->chip_select);
  637. tegra_spi_writel(tspi, command1, SPI_COMMAND1);
  638. tspi->command1_reg = command1;
  639. dev_dbg(tspi->dev, "The def 0x%x and written 0x%x\n",
  640. tspi->def_command1_reg, (unsigned)command1);
  641. if (total_fifo_words > SPI_FIFO_DEPTH)
  642. ret = tegra_spi_start_dma_based_transfer(tspi, t);
  643. else
  644. ret = tegra_spi_start_cpu_based_transfer(tspi, t);
  645. return ret;
  646. }
  647. static int tegra_spi_setup(struct spi_device *spi)
  648. {
  649. struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
  650. u32 val;
  651. unsigned long flags;
  652. int ret;
  653. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  654. spi->bits_per_word,
  655. spi->mode & SPI_CPOL ? "" : "~",
  656. spi->mode & SPI_CPHA ? "" : "~",
  657. spi->max_speed_hz);
  658. ret = pm_runtime_get_sync(tspi->dev);
  659. if (ret < 0) {
  660. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  661. return ret;
  662. }
  663. spin_lock_irqsave(&tspi->lock, flags);
  664. val = tspi->def_command1_reg;
  665. if (spi->mode & SPI_CS_HIGH)
  666. val &= ~SPI_CS_POL_INACTIVE(spi->chip_select);
  667. else
  668. val |= SPI_CS_POL_INACTIVE(spi->chip_select);
  669. tspi->def_command1_reg = val;
  670. tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
  671. spin_unlock_irqrestore(&tspi->lock, flags);
  672. pm_runtime_put(tspi->dev);
  673. return 0;
  674. }
  675. static void tegra_spi_transfer_delay(int delay)
  676. {
  677. if (!delay)
  678. return;
  679. if (delay >= 1000)
  680. mdelay(delay / 1000);
  681. udelay(delay % 1000);
  682. }
  683. static int tegra_spi_transfer_one_message(struct spi_master *master,
  684. struct spi_message *msg)
  685. {
  686. bool is_first_msg = true;
  687. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  688. struct spi_transfer *xfer;
  689. struct spi_device *spi = msg->spi;
  690. int ret;
  691. bool skip = false;
  692. msg->status = 0;
  693. msg->actual_length = 0;
  694. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  695. u32 cmd1;
  696. reinit_completion(&tspi->xfer_completion);
  697. cmd1 = tegra_spi_setup_transfer_one(spi, xfer, is_first_msg);
  698. if (!xfer->len) {
  699. ret = 0;
  700. skip = true;
  701. goto complete_xfer;
  702. }
  703. ret = tegra_spi_start_transfer_one(spi, xfer, cmd1);
  704. if (ret < 0) {
  705. dev_err(tspi->dev,
  706. "spi can not start transfer, err %d\n", ret);
  707. goto complete_xfer;
  708. }
  709. is_first_msg = false;
  710. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  711. SPI_DMA_TIMEOUT);
  712. if (WARN_ON(ret == 0)) {
  713. dev_err(tspi->dev,
  714. "spi trasfer timeout, err %d\n", ret);
  715. ret = -EIO;
  716. goto complete_xfer;
  717. }
  718. if (tspi->tx_status || tspi->rx_status) {
  719. dev_err(tspi->dev, "Error in Transfer\n");
  720. ret = -EIO;
  721. goto complete_xfer;
  722. }
  723. msg->actual_length += xfer->len;
  724. complete_xfer:
  725. if (ret < 0 || skip) {
  726. tegra_spi_writel(tspi, tspi->def_command1_reg,
  727. SPI_COMMAND1);
  728. tegra_spi_transfer_delay(xfer->delay_usecs);
  729. goto exit;
  730. } else if (list_is_last(&xfer->transfer_list,
  731. &msg->transfers)) {
  732. if (xfer->cs_change)
  733. tspi->cs_control = spi;
  734. else {
  735. tegra_spi_writel(tspi, tspi->def_command1_reg,
  736. SPI_COMMAND1);
  737. tegra_spi_transfer_delay(xfer->delay_usecs);
  738. }
  739. } else if (xfer->cs_change) {
  740. tegra_spi_writel(tspi, tspi->def_command1_reg,
  741. SPI_COMMAND1);
  742. tegra_spi_transfer_delay(xfer->delay_usecs);
  743. }
  744. }
  745. ret = 0;
  746. exit:
  747. msg->status = ret;
  748. spi_finalize_current_message(master);
  749. return ret;
  750. }
  751. static irqreturn_t handle_cpu_based_xfer(struct tegra_spi_data *tspi)
  752. {
  753. struct spi_transfer *t = tspi->curr_xfer;
  754. unsigned long flags;
  755. spin_lock_irqsave(&tspi->lock, flags);
  756. if (tspi->tx_status || tspi->rx_status) {
  757. dev_err(tspi->dev, "CpuXfer ERROR bit set 0x%x\n",
  758. tspi->status_reg);
  759. dev_err(tspi->dev, "CpuXfer 0x%08x:0x%08x\n",
  760. tspi->command1_reg, tspi->dma_control_reg);
  761. reset_control_assert(tspi->rst);
  762. udelay(2);
  763. reset_control_deassert(tspi->rst);
  764. complete(&tspi->xfer_completion);
  765. goto exit;
  766. }
  767. if (tspi->cur_direction & DATA_DIR_RX)
  768. tegra_spi_read_rx_fifo_to_client_rxbuf(tspi, t);
  769. if (tspi->cur_direction & DATA_DIR_TX)
  770. tspi->cur_pos = tspi->cur_tx_pos;
  771. else
  772. tspi->cur_pos = tspi->cur_rx_pos;
  773. if (tspi->cur_pos == t->len) {
  774. complete(&tspi->xfer_completion);
  775. goto exit;
  776. }
  777. tegra_spi_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  778. tegra_spi_start_cpu_based_transfer(tspi, t);
  779. exit:
  780. spin_unlock_irqrestore(&tspi->lock, flags);
  781. return IRQ_HANDLED;
  782. }
  783. static irqreturn_t handle_dma_based_xfer(struct tegra_spi_data *tspi)
  784. {
  785. struct spi_transfer *t = tspi->curr_xfer;
  786. long wait_status;
  787. int err = 0;
  788. unsigned total_fifo_words;
  789. unsigned long flags;
  790. /* Abort dmas if any error */
  791. if (tspi->cur_direction & DATA_DIR_TX) {
  792. if (tspi->tx_status) {
  793. dmaengine_terminate_all(tspi->tx_dma_chan);
  794. err += 1;
  795. } else {
  796. wait_status = wait_for_completion_interruptible_timeout(
  797. &tspi->tx_dma_complete, SPI_DMA_TIMEOUT);
  798. if (wait_status <= 0) {
  799. dmaengine_terminate_all(tspi->tx_dma_chan);
  800. dev_err(tspi->dev, "TxDma Xfer failed\n");
  801. err += 1;
  802. }
  803. }
  804. }
  805. if (tspi->cur_direction & DATA_DIR_RX) {
  806. if (tspi->rx_status) {
  807. dmaengine_terminate_all(tspi->rx_dma_chan);
  808. err += 2;
  809. } else {
  810. wait_status = wait_for_completion_interruptible_timeout(
  811. &tspi->rx_dma_complete, SPI_DMA_TIMEOUT);
  812. if (wait_status <= 0) {
  813. dmaengine_terminate_all(tspi->rx_dma_chan);
  814. dev_err(tspi->dev, "RxDma Xfer failed\n");
  815. err += 2;
  816. }
  817. }
  818. }
  819. spin_lock_irqsave(&tspi->lock, flags);
  820. if (err) {
  821. dev_err(tspi->dev, "DmaXfer: ERROR bit set 0x%x\n",
  822. tspi->status_reg);
  823. dev_err(tspi->dev, "DmaXfer 0x%08x:0x%08x\n",
  824. tspi->command1_reg, tspi->dma_control_reg);
  825. reset_control_assert(tspi->rst);
  826. udelay(2);
  827. reset_control_deassert(tspi->rst);
  828. complete(&tspi->xfer_completion);
  829. spin_unlock_irqrestore(&tspi->lock, flags);
  830. return IRQ_HANDLED;
  831. }
  832. if (tspi->cur_direction & DATA_DIR_RX)
  833. tegra_spi_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  834. if (tspi->cur_direction & DATA_DIR_TX)
  835. tspi->cur_pos = tspi->cur_tx_pos;
  836. else
  837. tspi->cur_pos = tspi->cur_rx_pos;
  838. if (tspi->cur_pos == t->len) {
  839. complete(&tspi->xfer_completion);
  840. goto exit;
  841. }
  842. /* Continue transfer in current message */
  843. total_fifo_words = tegra_spi_calculate_curr_xfer_param(tspi->cur_spi,
  844. tspi, t);
  845. if (total_fifo_words > SPI_FIFO_DEPTH)
  846. err = tegra_spi_start_dma_based_transfer(tspi, t);
  847. else
  848. err = tegra_spi_start_cpu_based_transfer(tspi, t);
  849. exit:
  850. spin_unlock_irqrestore(&tspi->lock, flags);
  851. return IRQ_HANDLED;
  852. }
  853. static irqreturn_t tegra_spi_isr_thread(int irq, void *context_data)
  854. {
  855. struct tegra_spi_data *tspi = context_data;
  856. if (!tspi->is_curr_dma_xfer)
  857. return handle_cpu_based_xfer(tspi);
  858. return handle_dma_based_xfer(tspi);
  859. }
  860. static irqreturn_t tegra_spi_isr(int irq, void *context_data)
  861. {
  862. struct tegra_spi_data *tspi = context_data;
  863. tspi->status_reg = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  864. if (tspi->cur_direction & DATA_DIR_TX)
  865. tspi->tx_status = tspi->status_reg &
  866. (SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF);
  867. if (tspi->cur_direction & DATA_DIR_RX)
  868. tspi->rx_status = tspi->status_reg &
  869. (SPI_RX_FIFO_OVF | SPI_RX_FIFO_UNF);
  870. tegra_spi_clear_status(tspi);
  871. return IRQ_WAKE_THREAD;
  872. }
  873. static const struct of_device_id tegra_spi_of_match[] = {
  874. { .compatible = "nvidia,tegra114-spi", },
  875. {}
  876. };
  877. MODULE_DEVICE_TABLE(of, tegra_spi_of_match);
  878. static int tegra_spi_probe(struct platform_device *pdev)
  879. {
  880. struct spi_master *master;
  881. struct tegra_spi_data *tspi;
  882. struct resource *r;
  883. int ret, spi_irq;
  884. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  885. if (!master) {
  886. dev_err(&pdev->dev, "master allocation failed\n");
  887. return -ENOMEM;
  888. }
  889. platform_set_drvdata(pdev, master);
  890. tspi = spi_master_get_devdata(master);
  891. if (of_property_read_u32(pdev->dev.of_node, "spi-max-frequency",
  892. &master->max_speed_hz))
  893. master->max_speed_hz = 25000000; /* 25MHz */
  894. /* the spi->mode bits understood by this driver: */
  895. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  896. master->setup = tegra_spi_setup;
  897. master->transfer_one_message = tegra_spi_transfer_one_message;
  898. master->num_chipselect = MAX_CHIP_SELECT;
  899. master->auto_runtime_pm = true;
  900. tspi->master = master;
  901. tspi->dev = &pdev->dev;
  902. spin_lock_init(&tspi->lock);
  903. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  904. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  905. if (IS_ERR(tspi->base)) {
  906. ret = PTR_ERR(tspi->base);
  907. goto exit_free_master;
  908. }
  909. tspi->phys = r->start;
  910. spi_irq = platform_get_irq(pdev, 0);
  911. tspi->irq = spi_irq;
  912. ret = request_threaded_irq(tspi->irq, tegra_spi_isr,
  913. tegra_spi_isr_thread, IRQF_ONESHOT,
  914. dev_name(&pdev->dev), tspi);
  915. if (ret < 0) {
  916. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  917. tspi->irq);
  918. goto exit_free_master;
  919. }
  920. tspi->clk = devm_clk_get(&pdev->dev, "spi");
  921. if (IS_ERR(tspi->clk)) {
  922. dev_err(&pdev->dev, "can not get clock\n");
  923. ret = PTR_ERR(tspi->clk);
  924. goto exit_free_irq;
  925. }
  926. tspi->rst = devm_reset_control_get(&pdev->dev, "spi");
  927. if (IS_ERR(tspi->rst)) {
  928. dev_err(&pdev->dev, "can not get reset\n");
  929. ret = PTR_ERR(tspi->rst);
  930. goto exit_free_irq;
  931. }
  932. tspi->max_buf_size = SPI_FIFO_DEPTH << 2;
  933. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  934. ret = tegra_spi_init_dma_param(tspi, true);
  935. if (ret < 0)
  936. goto exit_free_irq;
  937. ret = tegra_spi_init_dma_param(tspi, false);
  938. if (ret < 0)
  939. goto exit_rx_dma_free;
  940. tspi->max_buf_size = tspi->dma_buf_size;
  941. init_completion(&tspi->tx_dma_complete);
  942. init_completion(&tspi->rx_dma_complete);
  943. init_completion(&tspi->xfer_completion);
  944. pm_runtime_enable(&pdev->dev);
  945. if (!pm_runtime_enabled(&pdev->dev)) {
  946. ret = tegra_spi_runtime_resume(&pdev->dev);
  947. if (ret)
  948. goto exit_pm_disable;
  949. }
  950. ret = pm_runtime_get_sync(&pdev->dev);
  951. if (ret < 0) {
  952. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  953. goto exit_pm_disable;
  954. }
  955. tspi->def_command1_reg = SPI_M_S;
  956. tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
  957. pm_runtime_put(&pdev->dev);
  958. master->dev.of_node = pdev->dev.of_node;
  959. ret = devm_spi_register_master(&pdev->dev, master);
  960. if (ret < 0) {
  961. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  962. goto exit_pm_disable;
  963. }
  964. return ret;
  965. exit_pm_disable:
  966. pm_runtime_disable(&pdev->dev);
  967. if (!pm_runtime_status_suspended(&pdev->dev))
  968. tegra_spi_runtime_suspend(&pdev->dev);
  969. tegra_spi_deinit_dma_param(tspi, false);
  970. exit_rx_dma_free:
  971. tegra_spi_deinit_dma_param(tspi, true);
  972. exit_free_irq:
  973. free_irq(spi_irq, tspi);
  974. exit_free_master:
  975. spi_master_put(master);
  976. return ret;
  977. }
  978. static int tegra_spi_remove(struct platform_device *pdev)
  979. {
  980. struct spi_master *master = platform_get_drvdata(pdev);
  981. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  982. free_irq(tspi->irq, tspi);
  983. if (tspi->tx_dma_chan)
  984. tegra_spi_deinit_dma_param(tspi, false);
  985. if (tspi->rx_dma_chan)
  986. tegra_spi_deinit_dma_param(tspi, true);
  987. pm_runtime_disable(&pdev->dev);
  988. if (!pm_runtime_status_suspended(&pdev->dev))
  989. tegra_spi_runtime_suspend(&pdev->dev);
  990. return 0;
  991. }
  992. #ifdef CONFIG_PM_SLEEP
  993. static int tegra_spi_suspend(struct device *dev)
  994. {
  995. struct spi_master *master = dev_get_drvdata(dev);
  996. return spi_master_suspend(master);
  997. }
  998. static int tegra_spi_resume(struct device *dev)
  999. {
  1000. struct spi_master *master = dev_get_drvdata(dev);
  1001. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1002. int ret;
  1003. ret = pm_runtime_get_sync(dev);
  1004. if (ret < 0) {
  1005. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1006. return ret;
  1007. }
  1008. tegra_spi_writel(tspi, tspi->command1_reg, SPI_COMMAND1);
  1009. pm_runtime_put(dev);
  1010. return spi_master_resume(master);
  1011. }
  1012. #endif
  1013. static int tegra_spi_runtime_suspend(struct device *dev)
  1014. {
  1015. struct spi_master *master = dev_get_drvdata(dev);
  1016. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1017. /* Flush all write which are in PPSB queue by reading back */
  1018. tegra_spi_readl(tspi, SPI_COMMAND1);
  1019. clk_disable_unprepare(tspi->clk);
  1020. return 0;
  1021. }
  1022. static int tegra_spi_runtime_resume(struct device *dev)
  1023. {
  1024. struct spi_master *master = dev_get_drvdata(dev);
  1025. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1026. int ret;
  1027. ret = clk_prepare_enable(tspi->clk);
  1028. if (ret < 0) {
  1029. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1030. return ret;
  1031. }
  1032. return 0;
  1033. }
  1034. static const struct dev_pm_ops tegra_spi_pm_ops = {
  1035. SET_RUNTIME_PM_OPS(tegra_spi_runtime_suspend,
  1036. tegra_spi_runtime_resume, NULL)
  1037. SET_SYSTEM_SLEEP_PM_OPS(tegra_spi_suspend, tegra_spi_resume)
  1038. };
  1039. static struct platform_driver tegra_spi_driver = {
  1040. .driver = {
  1041. .name = "spi-tegra114",
  1042. .pm = &tegra_spi_pm_ops,
  1043. .of_match_table = tegra_spi_of_match,
  1044. },
  1045. .probe = tegra_spi_probe,
  1046. .remove = tegra_spi_remove,
  1047. };
  1048. module_platform_driver(tegra_spi_driver);
  1049. MODULE_ALIAS("platform:spi-tegra114");
  1050. MODULE_DESCRIPTION("NVIDIA Tegra114 SPI Controller Driver");
  1051. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1052. MODULE_LICENSE("GPL v2");