spi-tegra20-sflash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * SPI driver for Nvidia's Tegra20 Serial Flash Controller.
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/completion.h>
  22. #include <linux/delay.h>
  23. #include <linux/err.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/kernel.h>
  27. #include <linux/kthread.h>
  28. #include <linux/module.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include <linux/reset.h>
  34. #include <linux/spi/spi.h>
  35. #define SPI_COMMAND 0x000
  36. #define SPI_GO BIT(30)
  37. #define SPI_M_S BIT(28)
  38. #define SPI_ACTIVE_SCLK_MASK (0x3 << 26)
  39. #define SPI_ACTIVE_SCLK_DRIVE_LOW (0 << 26)
  40. #define SPI_ACTIVE_SCLK_DRIVE_HIGH (1 << 26)
  41. #define SPI_ACTIVE_SCLK_PULL_LOW (2 << 26)
  42. #define SPI_ACTIVE_SCLK_PULL_HIGH (3 << 26)
  43. #define SPI_CK_SDA_FALLING (1 << 21)
  44. #define SPI_CK_SDA_RISING (0 << 21)
  45. #define SPI_CK_SDA_MASK (1 << 21)
  46. #define SPI_ACTIVE_SDA (0x3 << 18)
  47. #define SPI_ACTIVE_SDA_DRIVE_LOW (0 << 18)
  48. #define SPI_ACTIVE_SDA_DRIVE_HIGH (1 << 18)
  49. #define SPI_ACTIVE_SDA_PULL_LOW (2 << 18)
  50. #define SPI_ACTIVE_SDA_PULL_HIGH (3 << 18)
  51. #define SPI_CS_POL_INVERT BIT(16)
  52. #define SPI_TX_EN BIT(15)
  53. #define SPI_RX_EN BIT(14)
  54. #define SPI_CS_VAL_HIGH BIT(13)
  55. #define SPI_CS_VAL_LOW 0x0
  56. #define SPI_CS_SW BIT(12)
  57. #define SPI_CS_HW 0x0
  58. #define SPI_CS_DELAY_MASK (7 << 9)
  59. #define SPI_CS3_EN BIT(8)
  60. #define SPI_CS2_EN BIT(7)
  61. #define SPI_CS1_EN BIT(6)
  62. #define SPI_CS0_EN BIT(5)
  63. #define SPI_CS_MASK (SPI_CS3_EN | SPI_CS2_EN | \
  64. SPI_CS1_EN | SPI_CS0_EN)
  65. #define SPI_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  66. #define SPI_MODES (SPI_ACTIVE_SCLK_MASK | SPI_CK_SDA_MASK)
  67. #define SPI_STATUS 0x004
  68. #define SPI_BSY BIT(31)
  69. #define SPI_RDY BIT(30)
  70. #define SPI_TXF_FLUSH BIT(29)
  71. #define SPI_RXF_FLUSH BIT(28)
  72. #define SPI_RX_UNF BIT(27)
  73. #define SPI_TX_OVF BIT(26)
  74. #define SPI_RXF_EMPTY BIT(25)
  75. #define SPI_RXF_FULL BIT(24)
  76. #define SPI_TXF_EMPTY BIT(23)
  77. #define SPI_TXF_FULL BIT(22)
  78. #define SPI_BLK_CNT(count) (((count) & 0xffff) + 1)
  79. #define SPI_FIFO_ERROR (SPI_RX_UNF | SPI_TX_OVF)
  80. #define SPI_FIFO_EMPTY (SPI_TX_EMPTY | SPI_RX_EMPTY)
  81. #define SPI_RX_CMP 0x8
  82. #define SPI_DMA_CTL 0x0C
  83. #define SPI_DMA_EN BIT(31)
  84. #define SPI_IE_RXC BIT(27)
  85. #define SPI_IE_TXC BIT(26)
  86. #define SPI_PACKED BIT(20)
  87. #define SPI_RX_TRIG_MASK (0x3 << 18)
  88. #define SPI_RX_TRIG_1W (0x0 << 18)
  89. #define SPI_RX_TRIG_4W (0x1 << 18)
  90. #define SPI_TX_TRIG_MASK (0x3 << 16)
  91. #define SPI_TX_TRIG_1W (0x0 << 16)
  92. #define SPI_TX_TRIG_4W (0x1 << 16)
  93. #define SPI_DMA_BLK_COUNT(count) (((count) - 1) & 0xFFFF)
  94. #define SPI_TX_FIFO 0x10
  95. #define SPI_RX_FIFO 0x20
  96. #define DATA_DIR_TX (1 << 0)
  97. #define DATA_DIR_RX (1 << 1)
  98. #define MAX_CHIP_SELECT 4
  99. #define SPI_FIFO_DEPTH 4
  100. #define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
  101. struct tegra_sflash_data {
  102. struct device *dev;
  103. struct spi_master *master;
  104. spinlock_t lock;
  105. struct clk *clk;
  106. struct reset_control *rst;
  107. void __iomem *base;
  108. unsigned irq;
  109. u32 cur_speed;
  110. struct spi_device *cur_spi;
  111. unsigned cur_pos;
  112. unsigned cur_len;
  113. unsigned bytes_per_word;
  114. unsigned cur_direction;
  115. unsigned curr_xfer_words;
  116. unsigned cur_rx_pos;
  117. unsigned cur_tx_pos;
  118. u32 tx_status;
  119. u32 rx_status;
  120. u32 status_reg;
  121. u32 def_command_reg;
  122. u32 command_reg;
  123. u32 dma_control_reg;
  124. struct completion xfer_completion;
  125. struct spi_transfer *curr_xfer;
  126. };
  127. static int tegra_sflash_runtime_suspend(struct device *dev);
  128. static int tegra_sflash_runtime_resume(struct device *dev);
  129. static inline u32 tegra_sflash_readl(struct tegra_sflash_data *tsd,
  130. unsigned long reg)
  131. {
  132. return readl(tsd->base + reg);
  133. }
  134. static inline void tegra_sflash_writel(struct tegra_sflash_data *tsd,
  135. u32 val, unsigned long reg)
  136. {
  137. writel(val, tsd->base + reg);
  138. }
  139. static void tegra_sflash_clear_status(struct tegra_sflash_data *tsd)
  140. {
  141. /* Write 1 to clear status register */
  142. tegra_sflash_writel(tsd, SPI_RDY | SPI_FIFO_ERROR, SPI_STATUS);
  143. }
  144. static unsigned tegra_sflash_calculate_curr_xfer_param(
  145. struct spi_device *spi, struct tegra_sflash_data *tsd,
  146. struct spi_transfer *t)
  147. {
  148. unsigned remain_len = t->len - tsd->cur_pos;
  149. unsigned max_word;
  150. tsd->bytes_per_word = DIV_ROUND_UP(t->bits_per_word, 8);
  151. max_word = remain_len / tsd->bytes_per_word;
  152. if (max_word > SPI_FIFO_DEPTH)
  153. max_word = SPI_FIFO_DEPTH;
  154. tsd->curr_xfer_words = max_word;
  155. return max_word;
  156. }
  157. static unsigned tegra_sflash_fill_tx_fifo_from_client_txbuf(
  158. struct tegra_sflash_data *tsd, struct spi_transfer *t)
  159. {
  160. unsigned nbytes;
  161. u32 status;
  162. unsigned max_n_32bit = tsd->curr_xfer_words;
  163. u8 *tx_buf = (u8 *)t->tx_buf + tsd->cur_tx_pos;
  164. if (max_n_32bit > SPI_FIFO_DEPTH)
  165. max_n_32bit = SPI_FIFO_DEPTH;
  166. nbytes = max_n_32bit * tsd->bytes_per_word;
  167. status = tegra_sflash_readl(tsd, SPI_STATUS);
  168. while (!(status & SPI_TXF_FULL)) {
  169. int i;
  170. u32 x = 0;
  171. for (i = 0; nbytes && (i < tsd->bytes_per_word);
  172. i++, nbytes--)
  173. x |= (u32)(*tx_buf++) << (i * 8);
  174. tegra_sflash_writel(tsd, x, SPI_TX_FIFO);
  175. if (!nbytes)
  176. break;
  177. status = tegra_sflash_readl(tsd, SPI_STATUS);
  178. }
  179. tsd->cur_tx_pos += max_n_32bit * tsd->bytes_per_word;
  180. return max_n_32bit;
  181. }
  182. static int tegra_sflash_read_rx_fifo_to_client_rxbuf(
  183. struct tegra_sflash_data *tsd, struct spi_transfer *t)
  184. {
  185. u32 status;
  186. unsigned int read_words = 0;
  187. u8 *rx_buf = (u8 *)t->rx_buf + tsd->cur_rx_pos;
  188. status = tegra_sflash_readl(tsd, SPI_STATUS);
  189. while (!(status & SPI_RXF_EMPTY)) {
  190. int i;
  191. u32 x = tegra_sflash_readl(tsd, SPI_RX_FIFO);
  192. for (i = 0; (i < tsd->bytes_per_word); i++)
  193. *rx_buf++ = (x >> (i*8)) & 0xFF;
  194. read_words++;
  195. status = tegra_sflash_readl(tsd, SPI_STATUS);
  196. }
  197. tsd->cur_rx_pos += read_words * tsd->bytes_per_word;
  198. return 0;
  199. }
  200. static int tegra_sflash_start_cpu_based_transfer(
  201. struct tegra_sflash_data *tsd, struct spi_transfer *t)
  202. {
  203. u32 val = 0;
  204. unsigned cur_words;
  205. if (tsd->cur_direction & DATA_DIR_TX)
  206. val |= SPI_IE_TXC;
  207. if (tsd->cur_direction & DATA_DIR_RX)
  208. val |= SPI_IE_RXC;
  209. tegra_sflash_writel(tsd, val, SPI_DMA_CTL);
  210. tsd->dma_control_reg = val;
  211. if (tsd->cur_direction & DATA_DIR_TX)
  212. cur_words = tegra_sflash_fill_tx_fifo_from_client_txbuf(tsd, t);
  213. else
  214. cur_words = tsd->curr_xfer_words;
  215. val |= SPI_DMA_BLK_COUNT(cur_words);
  216. tegra_sflash_writel(tsd, val, SPI_DMA_CTL);
  217. tsd->dma_control_reg = val;
  218. val |= SPI_DMA_EN;
  219. tegra_sflash_writel(tsd, val, SPI_DMA_CTL);
  220. return 0;
  221. }
  222. static int tegra_sflash_start_transfer_one(struct spi_device *spi,
  223. struct spi_transfer *t, bool is_first_of_msg,
  224. bool is_single_xfer)
  225. {
  226. struct tegra_sflash_data *tsd = spi_master_get_devdata(spi->master);
  227. u32 speed;
  228. u32 command;
  229. speed = t->speed_hz;
  230. if (speed != tsd->cur_speed) {
  231. clk_set_rate(tsd->clk, speed);
  232. tsd->cur_speed = speed;
  233. }
  234. tsd->cur_spi = spi;
  235. tsd->cur_pos = 0;
  236. tsd->cur_rx_pos = 0;
  237. tsd->cur_tx_pos = 0;
  238. tsd->curr_xfer = t;
  239. tegra_sflash_calculate_curr_xfer_param(spi, tsd, t);
  240. if (is_first_of_msg) {
  241. command = tsd->def_command_reg;
  242. command |= SPI_BIT_LENGTH(t->bits_per_word - 1);
  243. command |= SPI_CS_VAL_HIGH;
  244. command &= ~SPI_MODES;
  245. if (spi->mode & SPI_CPHA)
  246. command |= SPI_CK_SDA_FALLING;
  247. if (spi->mode & SPI_CPOL)
  248. command |= SPI_ACTIVE_SCLK_DRIVE_HIGH;
  249. else
  250. command |= SPI_ACTIVE_SCLK_DRIVE_LOW;
  251. command |= SPI_CS0_EN << spi->chip_select;
  252. } else {
  253. command = tsd->command_reg;
  254. command &= ~SPI_BIT_LENGTH(~0);
  255. command |= SPI_BIT_LENGTH(t->bits_per_word - 1);
  256. command &= ~(SPI_RX_EN | SPI_TX_EN);
  257. }
  258. tsd->cur_direction = 0;
  259. if (t->rx_buf) {
  260. command |= SPI_RX_EN;
  261. tsd->cur_direction |= DATA_DIR_RX;
  262. }
  263. if (t->tx_buf) {
  264. command |= SPI_TX_EN;
  265. tsd->cur_direction |= DATA_DIR_TX;
  266. }
  267. tegra_sflash_writel(tsd, command, SPI_COMMAND);
  268. tsd->command_reg = command;
  269. return tegra_sflash_start_cpu_based_transfer(tsd, t);
  270. }
  271. static int tegra_sflash_transfer_one_message(struct spi_master *master,
  272. struct spi_message *msg)
  273. {
  274. bool is_first_msg = true;
  275. int single_xfer;
  276. struct tegra_sflash_data *tsd = spi_master_get_devdata(master);
  277. struct spi_transfer *xfer;
  278. struct spi_device *spi = msg->spi;
  279. int ret;
  280. msg->status = 0;
  281. msg->actual_length = 0;
  282. single_xfer = list_is_singular(&msg->transfers);
  283. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  284. reinit_completion(&tsd->xfer_completion);
  285. ret = tegra_sflash_start_transfer_one(spi, xfer,
  286. is_first_msg, single_xfer);
  287. if (ret < 0) {
  288. dev_err(tsd->dev,
  289. "spi can not start transfer, err %d\n", ret);
  290. goto exit;
  291. }
  292. is_first_msg = false;
  293. ret = wait_for_completion_timeout(&tsd->xfer_completion,
  294. SPI_DMA_TIMEOUT);
  295. if (WARN_ON(ret == 0)) {
  296. dev_err(tsd->dev,
  297. "spi transfer timeout, err %d\n", ret);
  298. ret = -EIO;
  299. goto exit;
  300. }
  301. if (tsd->tx_status || tsd->rx_status) {
  302. dev_err(tsd->dev, "Error in Transfer\n");
  303. ret = -EIO;
  304. goto exit;
  305. }
  306. msg->actual_length += xfer->len;
  307. if (xfer->cs_change && xfer->delay_usecs) {
  308. tegra_sflash_writel(tsd, tsd->def_command_reg,
  309. SPI_COMMAND);
  310. udelay(xfer->delay_usecs);
  311. }
  312. }
  313. ret = 0;
  314. exit:
  315. tegra_sflash_writel(tsd, tsd->def_command_reg, SPI_COMMAND);
  316. msg->status = ret;
  317. spi_finalize_current_message(master);
  318. return ret;
  319. }
  320. static irqreturn_t handle_cpu_based_xfer(struct tegra_sflash_data *tsd)
  321. {
  322. struct spi_transfer *t = tsd->curr_xfer;
  323. unsigned long flags;
  324. spin_lock_irqsave(&tsd->lock, flags);
  325. if (tsd->tx_status || tsd->rx_status || (tsd->status_reg & SPI_BSY)) {
  326. dev_err(tsd->dev,
  327. "CpuXfer ERROR bit set 0x%x\n", tsd->status_reg);
  328. dev_err(tsd->dev,
  329. "CpuXfer 0x%08x:0x%08x\n", tsd->command_reg,
  330. tsd->dma_control_reg);
  331. reset_control_assert(tsd->rst);
  332. udelay(2);
  333. reset_control_deassert(tsd->rst);
  334. complete(&tsd->xfer_completion);
  335. goto exit;
  336. }
  337. if (tsd->cur_direction & DATA_DIR_RX)
  338. tegra_sflash_read_rx_fifo_to_client_rxbuf(tsd, t);
  339. if (tsd->cur_direction & DATA_DIR_TX)
  340. tsd->cur_pos = tsd->cur_tx_pos;
  341. else
  342. tsd->cur_pos = tsd->cur_rx_pos;
  343. if (tsd->cur_pos == t->len) {
  344. complete(&tsd->xfer_completion);
  345. goto exit;
  346. }
  347. tegra_sflash_calculate_curr_xfer_param(tsd->cur_spi, tsd, t);
  348. tegra_sflash_start_cpu_based_transfer(tsd, t);
  349. exit:
  350. spin_unlock_irqrestore(&tsd->lock, flags);
  351. return IRQ_HANDLED;
  352. }
  353. static irqreturn_t tegra_sflash_isr(int irq, void *context_data)
  354. {
  355. struct tegra_sflash_data *tsd = context_data;
  356. tsd->status_reg = tegra_sflash_readl(tsd, SPI_STATUS);
  357. if (tsd->cur_direction & DATA_DIR_TX)
  358. tsd->tx_status = tsd->status_reg & SPI_TX_OVF;
  359. if (tsd->cur_direction & DATA_DIR_RX)
  360. tsd->rx_status = tsd->status_reg & SPI_RX_UNF;
  361. tegra_sflash_clear_status(tsd);
  362. return handle_cpu_based_xfer(tsd);
  363. }
  364. static const struct of_device_id tegra_sflash_of_match[] = {
  365. { .compatible = "nvidia,tegra20-sflash", },
  366. {}
  367. };
  368. MODULE_DEVICE_TABLE(of, tegra_sflash_of_match);
  369. static int tegra_sflash_probe(struct platform_device *pdev)
  370. {
  371. struct spi_master *master;
  372. struct tegra_sflash_data *tsd;
  373. struct resource *r;
  374. int ret;
  375. const struct of_device_id *match;
  376. match = of_match_device(tegra_sflash_of_match, &pdev->dev);
  377. if (!match) {
  378. dev_err(&pdev->dev, "Error: No device match found\n");
  379. return -ENODEV;
  380. }
  381. master = spi_alloc_master(&pdev->dev, sizeof(*tsd));
  382. if (!master) {
  383. dev_err(&pdev->dev, "master allocation failed\n");
  384. return -ENOMEM;
  385. }
  386. /* the spi->mode bits understood by this driver: */
  387. master->mode_bits = SPI_CPOL | SPI_CPHA;
  388. master->transfer_one_message = tegra_sflash_transfer_one_message;
  389. master->auto_runtime_pm = true;
  390. master->num_chipselect = MAX_CHIP_SELECT;
  391. platform_set_drvdata(pdev, master);
  392. tsd = spi_master_get_devdata(master);
  393. tsd->master = master;
  394. tsd->dev = &pdev->dev;
  395. spin_lock_init(&tsd->lock);
  396. if (of_property_read_u32(tsd->dev->of_node, "spi-max-frequency",
  397. &master->max_speed_hz))
  398. master->max_speed_hz = 25000000; /* 25MHz */
  399. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  400. tsd->base = devm_ioremap_resource(&pdev->dev, r);
  401. if (IS_ERR(tsd->base)) {
  402. ret = PTR_ERR(tsd->base);
  403. goto exit_free_master;
  404. }
  405. tsd->irq = platform_get_irq(pdev, 0);
  406. ret = request_irq(tsd->irq, tegra_sflash_isr, 0,
  407. dev_name(&pdev->dev), tsd);
  408. if (ret < 0) {
  409. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  410. tsd->irq);
  411. goto exit_free_master;
  412. }
  413. tsd->clk = devm_clk_get(&pdev->dev, NULL);
  414. if (IS_ERR(tsd->clk)) {
  415. dev_err(&pdev->dev, "can not get clock\n");
  416. ret = PTR_ERR(tsd->clk);
  417. goto exit_free_irq;
  418. }
  419. tsd->rst = devm_reset_control_get_exclusive(&pdev->dev, "spi");
  420. if (IS_ERR(tsd->rst)) {
  421. dev_err(&pdev->dev, "can not get reset\n");
  422. ret = PTR_ERR(tsd->rst);
  423. goto exit_free_irq;
  424. }
  425. init_completion(&tsd->xfer_completion);
  426. pm_runtime_enable(&pdev->dev);
  427. if (!pm_runtime_enabled(&pdev->dev)) {
  428. ret = tegra_sflash_runtime_resume(&pdev->dev);
  429. if (ret)
  430. goto exit_pm_disable;
  431. }
  432. ret = pm_runtime_get_sync(&pdev->dev);
  433. if (ret < 0) {
  434. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  435. goto exit_pm_disable;
  436. }
  437. /* Reset controller */
  438. reset_control_assert(tsd->rst);
  439. udelay(2);
  440. reset_control_deassert(tsd->rst);
  441. tsd->def_command_reg = SPI_M_S | SPI_CS_SW;
  442. tegra_sflash_writel(tsd, tsd->def_command_reg, SPI_COMMAND);
  443. pm_runtime_put(&pdev->dev);
  444. master->dev.of_node = pdev->dev.of_node;
  445. ret = devm_spi_register_master(&pdev->dev, master);
  446. if (ret < 0) {
  447. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  448. goto exit_pm_disable;
  449. }
  450. return ret;
  451. exit_pm_disable:
  452. pm_runtime_disable(&pdev->dev);
  453. if (!pm_runtime_status_suspended(&pdev->dev))
  454. tegra_sflash_runtime_suspend(&pdev->dev);
  455. exit_free_irq:
  456. free_irq(tsd->irq, tsd);
  457. exit_free_master:
  458. spi_master_put(master);
  459. return ret;
  460. }
  461. static int tegra_sflash_remove(struct platform_device *pdev)
  462. {
  463. struct spi_master *master = platform_get_drvdata(pdev);
  464. struct tegra_sflash_data *tsd = spi_master_get_devdata(master);
  465. free_irq(tsd->irq, tsd);
  466. pm_runtime_disable(&pdev->dev);
  467. if (!pm_runtime_status_suspended(&pdev->dev))
  468. tegra_sflash_runtime_suspend(&pdev->dev);
  469. return 0;
  470. }
  471. #ifdef CONFIG_PM_SLEEP
  472. static int tegra_sflash_suspend(struct device *dev)
  473. {
  474. struct spi_master *master = dev_get_drvdata(dev);
  475. return spi_master_suspend(master);
  476. }
  477. static int tegra_sflash_resume(struct device *dev)
  478. {
  479. struct spi_master *master = dev_get_drvdata(dev);
  480. struct tegra_sflash_data *tsd = spi_master_get_devdata(master);
  481. int ret;
  482. ret = pm_runtime_get_sync(dev);
  483. if (ret < 0) {
  484. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  485. return ret;
  486. }
  487. tegra_sflash_writel(tsd, tsd->command_reg, SPI_COMMAND);
  488. pm_runtime_put(dev);
  489. return spi_master_resume(master);
  490. }
  491. #endif
  492. static int tegra_sflash_runtime_suspend(struct device *dev)
  493. {
  494. struct spi_master *master = dev_get_drvdata(dev);
  495. struct tegra_sflash_data *tsd = spi_master_get_devdata(master);
  496. /* Flush all write which are in PPSB queue by reading back */
  497. tegra_sflash_readl(tsd, SPI_COMMAND);
  498. clk_disable_unprepare(tsd->clk);
  499. return 0;
  500. }
  501. static int tegra_sflash_runtime_resume(struct device *dev)
  502. {
  503. struct spi_master *master = dev_get_drvdata(dev);
  504. struct tegra_sflash_data *tsd = spi_master_get_devdata(master);
  505. int ret;
  506. ret = clk_prepare_enable(tsd->clk);
  507. if (ret < 0) {
  508. dev_err(tsd->dev, "clk_prepare failed: %d\n", ret);
  509. return ret;
  510. }
  511. return 0;
  512. }
  513. static const struct dev_pm_ops slink_pm_ops = {
  514. SET_RUNTIME_PM_OPS(tegra_sflash_runtime_suspend,
  515. tegra_sflash_runtime_resume, NULL)
  516. SET_SYSTEM_SLEEP_PM_OPS(tegra_sflash_suspend, tegra_sflash_resume)
  517. };
  518. static struct platform_driver tegra_sflash_driver = {
  519. .driver = {
  520. .name = "spi-tegra-sflash",
  521. .pm = &slink_pm_ops,
  522. .of_match_table = tegra_sflash_of_match,
  523. },
  524. .probe = tegra_sflash_probe,
  525. .remove = tegra_sflash_remove,
  526. };
  527. module_platform_driver(tegra_sflash_driver);
  528. MODULE_ALIAS("platform:spi-tegra-sflash");
  529. MODULE_DESCRIPTION("NVIDIA Tegra20 Serial Flash Controller Driver");
  530. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  531. MODULE_LICENSE("GPL v2");