spi-tegra114.c 33 KB

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