spi-tegra114.c 34 KB

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