spi-tegra114.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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 = (bits_per_word - 1) / 8 + 1;
  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_prepare_transfer(struct spi_master *spi)
  697. {
  698. struct tegra_spi_data *tspi = spi_master_get_devdata(spi);
  699. int ret;
  700. ret = pm_runtime_get_sync(tspi->dev);
  701. if (ret < 0) {
  702. dev_err(tspi->dev, "runtime PM get failed: %d\n", ret);
  703. return ret;
  704. }
  705. return ret;
  706. }
  707. static int tegra_spi_transfer_one_message(struct spi_master *master,
  708. struct spi_message *msg)
  709. {
  710. bool is_first_msg = true;
  711. int single_xfer;
  712. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  713. struct spi_transfer *xfer;
  714. struct spi_device *spi = msg->spi;
  715. int ret;
  716. msg->status = 0;
  717. msg->actual_length = 0;
  718. single_xfer = list_is_singular(&msg->transfers);
  719. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  720. INIT_COMPLETION(tspi->xfer_completion);
  721. ret = tegra_spi_start_transfer_one(spi, xfer,
  722. is_first_msg, single_xfer);
  723. if (ret < 0) {
  724. dev_err(tspi->dev,
  725. "spi can not start transfer, err %d\n", ret);
  726. goto exit;
  727. }
  728. is_first_msg = false;
  729. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  730. SPI_DMA_TIMEOUT);
  731. if (WARN_ON(ret == 0)) {
  732. dev_err(tspi->dev,
  733. "spi trasfer timeout, err %d\n", ret);
  734. ret = -EIO;
  735. goto exit;
  736. }
  737. if (tspi->tx_status || tspi->rx_status) {
  738. dev_err(tspi->dev, "Error in Transfer\n");
  739. ret = -EIO;
  740. goto exit;
  741. }
  742. msg->actual_length += xfer->len;
  743. if (xfer->cs_change && xfer->delay_usecs) {
  744. tegra_spi_writel(tspi, tspi->def_command1_reg,
  745. SPI_COMMAND1);
  746. udelay(xfer->delay_usecs);
  747. }
  748. }
  749. ret = 0;
  750. exit:
  751. tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
  752. msg->status = ret;
  753. spi_finalize_current_message(master);
  754. return ret;
  755. }
  756. static int tegra_spi_unprepare_transfer(struct spi_master *spi)
  757. {
  758. struct tegra_spi_data *tspi = spi_master_get_devdata(spi);
  759. pm_runtime_put(tspi->dev);
  760. return 0;
  761. }
  762. static irqreturn_t handle_cpu_based_xfer(struct tegra_spi_data *tspi)
  763. {
  764. struct spi_transfer *t = tspi->curr_xfer;
  765. unsigned long flags;
  766. spin_lock_irqsave(&tspi->lock, flags);
  767. if (tspi->tx_status || tspi->rx_status) {
  768. dev_err(tspi->dev, "CpuXfer ERROR bit set 0x%x\n",
  769. tspi->status_reg);
  770. dev_err(tspi->dev, "CpuXfer 0x%08x:0x%08x\n",
  771. tspi->command1_reg, tspi->dma_control_reg);
  772. tegra_periph_reset_assert(tspi->clk);
  773. udelay(2);
  774. tegra_periph_reset_deassert(tspi->clk);
  775. complete(&tspi->xfer_completion);
  776. goto exit;
  777. }
  778. if (tspi->cur_direction & DATA_DIR_RX)
  779. tegra_spi_read_rx_fifo_to_client_rxbuf(tspi, t);
  780. if (tspi->cur_direction & DATA_DIR_TX)
  781. tspi->cur_pos = tspi->cur_tx_pos;
  782. else
  783. tspi->cur_pos = tspi->cur_rx_pos;
  784. if (tspi->cur_pos == t->len) {
  785. complete(&tspi->xfer_completion);
  786. goto exit;
  787. }
  788. tegra_spi_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  789. tegra_spi_start_cpu_based_transfer(tspi, t);
  790. exit:
  791. spin_unlock_irqrestore(&tspi->lock, flags);
  792. return IRQ_HANDLED;
  793. }
  794. static irqreturn_t handle_dma_based_xfer(struct tegra_spi_data *tspi)
  795. {
  796. struct spi_transfer *t = tspi->curr_xfer;
  797. long wait_status;
  798. int err = 0;
  799. unsigned total_fifo_words;
  800. unsigned long flags;
  801. /* Abort dmas if any error */
  802. if (tspi->cur_direction & DATA_DIR_TX) {
  803. if (tspi->tx_status) {
  804. dmaengine_terminate_all(tspi->tx_dma_chan);
  805. err += 1;
  806. } else {
  807. wait_status = wait_for_completion_interruptible_timeout(
  808. &tspi->tx_dma_complete, SPI_DMA_TIMEOUT);
  809. if (wait_status <= 0) {
  810. dmaengine_terminate_all(tspi->tx_dma_chan);
  811. dev_err(tspi->dev, "TxDma Xfer failed\n");
  812. err += 1;
  813. }
  814. }
  815. }
  816. if (tspi->cur_direction & DATA_DIR_RX) {
  817. if (tspi->rx_status) {
  818. dmaengine_terminate_all(tspi->rx_dma_chan);
  819. err += 2;
  820. } else {
  821. wait_status = wait_for_completion_interruptible_timeout(
  822. &tspi->rx_dma_complete, SPI_DMA_TIMEOUT);
  823. if (wait_status <= 0) {
  824. dmaengine_terminate_all(tspi->rx_dma_chan);
  825. dev_err(tspi->dev, "RxDma Xfer failed\n");
  826. err += 2;
  827. }
  828. }
  829. }
  830. spin_lock_irqsave(&tspi->lock, flags);
  831. if (err) {
  832. dev_err(tspi->dev, "DmaXfer: ERROR bit set 0x%x\n",
  833. tspi->status_reg);
  834. dev_err(tspi->dev, "DmaXfer 0x%08x:0x%08x\n",
  835. tspi->command1_reg, tspi->dma_control_reg);
  836. tegra_periph_reset_assert(tspi->clk);
  837. udelay(2);
  838. tegra_periph_reset_deassert(tspi->clk);
  839. complete(&tspi->xfer_completion);
  840. spin_unlock_irqrestore(&tspi->lock, flags);
  841. return IRQ_HANDLED;
  842. }
  843. if (tspi->cur_direction & DATA_DIR_RX)
  844. tegra_spi_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  845. if (tspi->cur_direction & DATA_DIR_TX)
  846. tspi->cur_pos = tspi->cur_tx_pos;
  847. else
  848. tspi->cur_pos = tspi->cur_rx_pos;
  849. if (tspi->cur_pos == t->len) {
  850. complete(&tspi->xfer_completion);
  851. goto exit;
  852. }
  853. /* Continue transfer in current message */
  854. total_fifo_words = tegra_spi_calculate_curr_xfer_param(tspi->cur_spi,
  855. tspi, t);
  856. if (total_fifo_words > SPI_FIFO_DEPTH)
  857. err = tegra_spi_start_dma_based_transfer(tspi, t);
  858. else
  859. err = tegra_spi_start_cpu_based_transfer(tspi, t);
  860. exit:
  861. spin_unlock_irqrestore(&tspi->lock, flags);
  862. return IRQ_HANDLED;
  863. }
  864. static irqreturn_t tegra_spi_isr_thread(int irq, void *context_data)
  865. {
  866. struct tegra_spi_data *tspi = context_data;
  867. if (!tspi->is_curr_dma_xfer)
  868. return handle_cpu_based_xfer(tspi);
  869. return handle_dma_based_xfer(tspi);
  870. }
  871. static irqreturn_t tegra_spi_isr(int irq, void *context_data)
  872. {
  873. struct tegra_spi_data *tspi = context_data;
  874. tspi->status_reg = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
  875. if (tspi->cur_direction & DATA_DIR_TX)
  876. tspi->tx_status = tspi->status_reg &
  877. (SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF);
  878. if (tspi->cur_direction & DATA_DIR_RX)
  879. tspi->rx_status = tspi->status_reg &
  880. (SPI_RX_FIFO_OVF | SPI_RX_FIFO_UNF);
  881. tegra_spi_clear_status(tspi);
  882. return IRQ_WAKE_THREAD;
  883. }
  884. static void tegra_spi_parse_dt(struct platform_device *pdev,
  885. struct tegra_spi_data *tspi)
  886. {
  887. struct device_node *np = pdev->dev.of_node;
  888. u32 of_dma[2];
  889. if (of_property_read_u32_array(np, "nvidia,dma-request-selector",
  890. of_dma, 2) >= 0)
  891. tspi->dma_req_sel = of_dma[1];
  892. if (of_property_read_u32(np, "spi-max-frequency",
  893. &tspi->spi_max_frequency))
  894. tspi->spi_max_frequency = 25000000; /* 25MHz */
  895. }
  896. static struct of_device_id tegra_spi_of_match[] = {
  897. { .compatible = "nvidia,tegra114-spi", },
  898. {}
  899. };
  900. MODULE_DEVICE_TABLE(of, tegra_spi_of_match);
  901. static int tegra_spi_probe(struct platform_device *pdev)
  902. {
  903. struct spi_master *master;
  904. struct tegra_spi_data *tspi;
  905. struct resource *r;
  906. int ret, spi_irq;
  907. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  908. if (!master) {
  909. dev_err(&pdev->dev, "master allocation failed\n");
  910. return -ENOMEM;
  911. }
  912. platform_set_drvdata(pdev, master);
  913. tspi = spi_master_get_devdata(master);
  914. /* Parse DT */
  915. tegra_spi_parse_dt(pdev, tspi);
  916. /* the spi->mode bits understood by this driver: */
  917. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  918. master->setup = tegra_spi_setup;
  919. master->prepare_transfer_hardware = tegra_spi_prepare_transfer;
  920. master->transfer_one_message = tegra_spi_transfer_one_message;
  921. master->unprepare_transfer_hardware = tegra_spi_unprepare_transfer;
  922. master->num_chipselect = MAX_CHIP_SELECT;
  923. master->bus_num = -1;
  924. tspi->master = master;
  925. tspi->dev = &pdev->dev;
  926. spin_lock_init(&tspi->lock);
  927. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  928. if (!r) {
  929. dev_err(&pdev->dev, "No IO memory resource\n");
  930. ret = -ENODEV;
  931. goto exit_free_master;
  932. }
  933. tspi->phys = r->start;
  934. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  935. if (IS_ERR(tspi->base)) {
  936. ret = PTR_ERR(tspi->base);
  937. dev_err(&pdev->dev, "ioremap failed: err = %d\n", ret);
  938. goto exit_free_master;
  939. }
  940. spi_irq = platform_get_irq(pdev, 0);
  941. tspi->irq = spi_irq;
  942. ret = request_threaded_irq(tspi->irq, tegra_spi_isr,
  943. tegra_spi_isr_thread, IRQF_ONESHOT,
  944. dev_name(&pdev->dev), tspi);
  945. if (ret < 0) {
  946. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  947. tspi->irq);
  948. goto exit_free_master;
  949. }
  950. tspi->clk = devm_clk_get(&pdev->dev, "spi");
  951. if (IS_ERR(tspi->clk)) {
  952. dev_err(&pdev->dev, "can not get clock\n");
  953. ret = PTR_ERR(tspi->clk);
  954. goto exit_free_irq;
  955. }
  956. tspi->max_buf_size = SPI_FIFO_DEPTH << 2;
  957. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  958. if (tspi->dma_req_sel) {
  959. ret = tegra_spi_init_dma_param(tspi, true);
  960. if (ret < 0) {
  961. dev_err(&pdev->dev, "RxDma Init failed, err %d\n", ret);
  962. goto exit_free_irq;
  963. }
  964. ret = tegra_spi_init_dma_param(tspi, false);
  965. if (ret < 0) {
  966. dev_err(&pdev->dev, "TxDma Init failed, err %d\n", ret);
  967. goto exit_rx_dma_free;
  968. }
  969. tspi->max_buf_size = tspi->dma_buf_size;
  970. init_completion(&tspi->tx_dma_complete);
  971. init_completion(&tspi->rx_dma_complete);
  972. }
  973. init_completion(&tspi->xfer_completion);
  974. pm_runtime_enable(&pdev->dev);
  975. if (!pm_runtime_enabled(&pdev->dev)) {
  976. ret = tegra_spi_runtime_resume(&pdev->dev);
  977. if (ret)
  978. goto exit_pm_disable;
  979. }
  980. ret = pm_runtime_get_sync(&pdev->dev);
  981. if (ret < 0) {
  982. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  983. goto exit_pm_disable;
  984. }
  985. tspi->def_command1_reg = SPI_M_S;
  986. tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
  987. pm_runtime_put(&pdev->dev);
  988. master->dev.of_node = pdev->dev.of_node;
  989. ret = spi_register_master(master);
  990. if (ret < 0) {
  991. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  992. goto exit_pm_disable;
  993. }
  994. return ret;
  995. exit_pm_disable:
  996. pm_runtime_disable(&pdev->dev);
  997. if (!pm_runtime_status_suspended(&pdev->dev))
  998. tegra_spi_runtime_suspend(&pdev->dev);
  999. tegra_spi_deinit_dma_param(tspi, false);
  1000. exit_rx_dma_free:
  1001. tegra_spi_deinit_dma_param(tspi, true);
  1002. exit_free_irq:
  1003. free_irq(spi_irq, tspi);
  1004. exit_free_master:
  1005. spi_master_put(master);
  1006. return ret;
  1007. }
  1008. static int tegra_spi_remove(struct platform_device *pdev)
  1009. {
  1010. struct spi_master *master = platform_get_drvdata(pdev);
  1011. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1012. free_irq(tspi->irq, tspi);
  1013. spi_unregister_master(master);
  1014. if (tspi->tx_dma_chan)
  1015. tegra_spi_deinit_dma_param(tspi, false);
  1016. if (tspi->rx_dma_chan)
  1017. tegra_spi_deinit_dma_param(tspi, true);
  1018. pm_runtime_disable(&pdev->dev);
  1019. if (!pm_runtime_status_suspended(&pdev->dev))
  1020. tegra_spi_runtime_suspend(&pdev->dev);
  1021. return 0;
  1022. }
  1023. #ifdef CONFIG_PM_SLEEP
  1024. static int tegra_spi_suspend(struct device *dev)
  1025. {
  1026. struct spi_master *master = dev_get_drvdata(dev);
  1027. return spi_master_suspend(master);
  1028. }
  1029. static int tegra_spi_resume(struct device *dev)
  1030. {
  1031. struct spi_master *master = dev_get_drvdata(dev);
  1032. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1033. int ret;
  1034. ret = pm_runtime_get_sync(dev);
  1035. if (ret < 0) {
  1036. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1037. return ret;
  1038. }
  1039. tegra_spi_writel(tspi, tspi->command1_reg, SPI_COMMAND1);
  1040. pm_runtime_put(dev);
  1041. return spi_master_resume(master);
  1042. }
  1043. #endif
  1044. static int tegra_spi_runtime_suspend(struct device *dev)
  1045. {
  1046. struct spi_master *master = dev_get_drvdata(dev);
  1047. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1048. /* Flush all write which are in PPSB queue by reading back */
  1049. tegra_spi_readl(tspi, SPI_COMMAND1);
  1050. clk_disable_unprepare(tspi->clk);
  1051. return 0;
  1052. }
  1053. static int tegra_spi_runtime_resume(struct device *dev)
  1054. {
  1055. struct spi_master *master = dev_get_drvdata(dev);
  1056. struct tegra_spi_data *tspi = spi_master_get_devdata(master);
  1057. int ret;
  1058. ret = clk_prepare_enable(tspi->clk);
  1059. if (ret < 0) {
  1060. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1061. return ret;
  1062. }
  1063. return 0;
  1064. }
  1065. static const struct dev_pm_ops tegra_spi_pm_ops = {
  1066. SET_RUNTIME_PM_OPS(tegra_spi_runtime_suspend,
  1067. tegra_spi_runtime_resume, NULL)
  1068. SET_SYSTEM_SLEEP_PM_OPS(tegra_spi_suspend, tegra_spi_resume)
  1069. };
  1070. static struct platform_driver tegra_spi_driver = {
  1071. .driver = {
  1072. .name = "spi-tegra114",
  1073. .owner = THIS_MODULE,
  1074. .pm = &tegra_spi_pm_ops,
  1075. .of_match_table = tegra_spi_of_match,
  1076. },
  1077. .probe = tegra_spi_probe,
  1078. .remove = tegra_spi_remove,
  1079. };
  1080. module_platform_driver(tegra_spi_driver);
  1081. MODULE_ALIAS("platform:spi-tegra114");
  1082. MODULE_DESCRIPTION("NVIDIA Tegra114 SPI Controller Driver");
  1083. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1084. MODULE_LICENSE("GPL v2");