2 * SuperH HSPI bus driver
4 * Copyright (C) 2011 Kuninori Morimoto
7 * Based on pxa2xx_spi.c:
8 * Copyright (C) 2011 Renesas Solutions Corp.
9 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <linux/clk.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/timer.h>
30 #include <linux/delay.h>
31 #include <linux/list.h>
32 #include <linux/interrupt.h>
33 #include <linux/platform_device.h>
34 #include <linux/pm_runtime.h>
36 #include <linux/spi/spi.h>
37 #include <linux/spi/sh_hspi.h>
49 #define hspi2info(h) (h->dev->platform_data)
53 struct spi_master *master;
61 static void hspi_write(struct hspi_priv *hspi, int reg, u32 val)
63 iowrite32(val, hspi->addr + reg);
66 static u32 hspi_read(struct hspi_priv *hspi, int reg)
68 return ioread32(hspi->addr + reg);
71 static void hspi_bit_set(struct hspi_priv *hspi, int reg, u32 mask, u32 set)
73 u32 val = hspi_read(hspi, reg);
78 hspi_write(hspi, reg, val);
84 static int hspi_status_check_timeout(struct hspi_priv *hspi, u32 mask, u32 val)
89 if ((mask & hspi_read(hspi, SPSR)) == val)
95 dev_err(hspi->dev, "timeout\n");
100 * spi master function
102 static int hspi_prepare_transfer(struct spi_master *master)
104 struct hspi_priv *hspi = spi_master_get_devdata(master);
106 pm_runtime_get_sync(hspi->dev);
110 static int hspi_unprepare_transfer(struct spi_master *master)
112 struct hspi_priv *hspi = spi_master_get_devdata(master);
114 pm_runtime_put_sync(hspi->dev);
118 #define hspi_hw_cs_enable(hspi) hspi_hw_cs_ctrl(hspi, 0)
119 #define hspi_hw_cs_disable(hspi) hspi_hw_cs_ctrl(hspi, 1)
120 static void hspi_hw_cs_ctrl(struct hspi_priv *hspi, int hi)
122 hspi_bit_set(hspi, SPSCR, (1 << 6), (hi) << 6);
125 static void hspi_hw_setup(struct hspi_priv *hspi,
126 struct spi_message *msg,
127 struct spi_transfer *t)
129 struct spi_device *spi = msg->spi;
130 struct device *dev = hspi->dev;
133 u32 rate, best_rate, min, tmp;
135 target_rate = t ? t->speed_hz : 0;
137 target_rate = spi->max_speed_hz;
140 * find best IDIV/CLKCx settings
145 for (idiv_clk = 0x00; idiv_clk <= 0x3F; idiv_clk++) {
146 rate = clk_get_rate(hspi->clk);
148 /* IDIV calculation */
149 if (idiv_clk & (1 << 5))
154 /* CLKCx calculation */
155 rate /= (((idiv_clk & 0x1F) + 1) * 2) ;
157 /* save best settings */
158 tmp = abs(target_rate - rate);
166 if (spi->mode & SPI_CPHA)
168 if (spi->mode & SPI_CPOL)
171 dev_dbg(dev, "speed %d/%d\n", target_rate, best_rate);
173 hspi_write(hspi, SPCR, spcr);
174 hspi_write(hspi, SPSR, 0x0);
175 hspi_write(hspi, SPSCR, 0x21); /* master mode / CS control */
178 static int hspi_transfer_one_message(struct spi_master *master,
179 struct spi_message *msg)
181 struct hspi_priv *hspi = spi_master_get_devdata(master);
182 struct spi_transfer *t;
186 unsigned int cs_change;
187 const int nsecs = 50;
189 dev_dbg(hspi->dev, "%s\n", __func__);
193 list_for_each_entry(t, &msg->transfers, transfer_list) {
196 hspi_hw_setup(hspi, msg, t);
197 hspi_hw_cs_enable(hspi);
200 cs_change = t->cs_change;
202 for (i = 0; i < t->len; i++) {
205 ret = hspi_status_check_timeout(hspi, 0x1, 0);
211 tx = (u32)((u8 *)t->tx_buf)[i];
213 hspi_write(hspi, SPTBR, tx);
216 ret = hspi_status_check_timeout(hspi, 0x4, 0x4);
220 rx = hspi_read(hspi, SPRBR);
222 ((u8 *)t->rx_buf)[i] = (u8)rx;
226 msg->actual_length += t->len;
229 udelay(t->delay_usecs);
233 hspi_hw_cs_disable(hspi);
241 hspi_hw_cs_disable(hspi);
243 spi_finalize_current_message(master);
248 static int hspi_setup(struct spi_device *spi)
250 struct hspi_priv *hspi = spi_master_get_devdata(spi->master);
251 struct device *dev = hspi->dev;
253 if (8 != spi->bits_per_word) {
254 dev_err(dev, "bits_per_word should be 8\n");
258 dev_dbg(dev, "%s setup\n", spi->modalias);
263 static void hspi_cleanup(struct spi_device *spi)
265 struct hspi_priv *hspi = spi_master_get_devdata(spi->master);
266 struct device *dev = hspi->dev;
268 dev_dbg(dev, "%s cleanup\n", spi->modalias);
271 static int hspi_probe(struct platform_device *pdev)
273 struct resource *res;
274 struct spi_master *master;
275 struct hspi_priv *hspi;
280 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
282 dev_err(&pdev->dev, "invalid resource\n");
286 master = spi_alloc_master(&pdev->dev, sizeof(*hspi));
288 dev_err(&pdev->dev, "spi_alloc_master error.\n");
292 clk = clk_get(NULL, "shyway_clk");
294 dev_err(&pdev->dev, "shyway_clk is required\n");
299 hspi = spi_master_get_devdata(master);
300 platform_set_drvdata(pdev, hspi);
303 hspi->master = master;
304 hspi->dev = &pdev->dev;
306 hspi->addr = devm_ioremap(hspi->dev,
307 res->start, resource_size(res));
309 dev_err(&pdev->dev, "ioremap error.\n");
314 master->num_chipselect = 1;
315 master->bus_num = pdev->id;
316 master->setup = hspi_setup;
317 master->cleanup = hspi_cleanup;
318 master->mode_bits = SPI_CPOL | SPI_CPHA;
319 master->prepare_transfer_hardware = hspi_prepare_transfer;
320 master->transfer_one_message = hspi_transfer_one_message;
321 master->unprepare_transfer_hardware = hspi_unprepare_transfer;
322 ret = spi_register_master(master);
324 dev_err(&pdev->dev, "spi_register_master error.\n");
328 pm_runtime_enable(&pdev->dev);
330 dev_info(&pdev->dev, "probed\n");
337 spi_master_put(master);
342 static int hspi_remove(struct platform_device *pdev)
344 struct hspi_priv *hspi = platform_get_drvdata(pdev);
346 pm_runtime_disable(&pdev->dev);
349 spi_unregister_master(hspi->master);
354 static struct platform_driver hspi_driver = {
356 .remove = hspi_remove,
359 .owner = THIS_MODULE,
362 module_platform_driver(hspi_driver);
364 MODULE_DESCRIPTION("SuperH HSPI bus driver");
365 MODULE_LICENSE("GPL");
367 MODULE_ALIAS("platform:sh_spi");