2 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
29 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <geom/geom_disk.h>
43 #include <dev/spibus/spi.h>
44 #include "spibus_if.h"
48 struct intr_config_hook config_intrhook;
53 struct bio_queue_head bio_queue;
56 #define AT45D_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
57 #define AT45D_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
58 #define AT45D_LOCK_INIT(_sc) \
59 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
61 #define AT45D_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
62 #define AT45D_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
63 #define AT45D_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
65 static void at45d_delayed_attach(void *xsc);
68 static int at45d_open(struct disk *dp);
69 static int at45d_close(struct disk *dp);
70 static void at45d_strategy(struct bio *bp);
71 static void at45d_task(void *arg);
73 #define CONTINUOUS_ARRAY_READ 0xE8
74 #define CONTINUOUS_ARRAY_READ_HF 0x0B
75 #define CONTINUOUS_ARRAY_READ_LF 0x03
76 #define STATUS_REGISTER_READ 0xD7
77 #define PROGRAM_THROUGH_BUFFER 0x82
78 #define MANUFACTURER_ID 0x9F
81 at45d_get_status(device_t dev)
83 uint8_t txBuf[8], rxBuf[8];
84 struct spi_command cmd;
87 memset(&cmd, 0, sizeof(cmd));
88 memset(txBuf, 0, sizeof(txBuf));
89 memset(rxBuf, 0, sizeof(rxBuf));
91 txBuf[0] = STATUS_REGISTER_READ;
96 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
101 at45d_wait_for_device_ready(device_t dev)
103 while (!(at45d_get_status(dev) & 0x80))
108 at45d_get_mfg_info(device_t dev, uint8_t *resp)
110 uint8_t txBuf[8], rxBuf[8];
111 struct spi_command cmd;
114 memset(&cmd, 0, sizeof(cmd));
115 memset(txBuf, 0, sizeof(txBuf));
116 memset(rxBuf, 0, sizeof(rxBuf));
118 txBuf[0] = MANUFACTURER_ID;
123 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
126 memcpy(resp, rxBuf + 1, 4);
127 // XXX We really should 'decode' the reply into some kind of
128 // XXX structure. To be generic (and not just for atmel parts)
129 // XXX we'd have to loop until we got a full reply.
134 at45d_probe(device_t dev)
136 device_set_desc(dev, "AT45 Flash Family");
141 at45d_attach(device_t dev)
143 struct at45d_softc *sc;
145 sc = device_get_softc(dev);
149 /* We'll see what kind of flash we have later... */
150 sc->config_intrhook.ich_func = at45d_delayed_attach;
151 sc->config_intrhook.ich_arg = sc;
152 if (config_intrhook_establish(&sc->config_intrhook) != 0)
153 device_printf(dev, "config_intrhook_establish failed\n");
158 at45d_detach(device_t dev)
164 at45d_delayed_attach(void *xsc)
166 struct at45d_softc *sc = xsc;
169 at45d_get_mfg_info(sc->dev, buf);
170 at45d_wait_for_device_ready(sc->dev);
172 sc->disk = disk_alloc();
173 sc->disk->d_open = at45d_open;
174 sc->disk->d_close = at45d_close;
175 sc->disk->d_strategy = at45d_strategy;
176 sc->disk->d_name = "flash/spi";
177 sc->disk->d_drv1 = sc;
178 sc->disk->d_maxsize = DFLTPHYS;
179 sc->disk->d_sectorsize = 1056; /* XXX */
180 sc->disk->d_mediasize = 8192 * 1056; /* XXX */
181 sc->disk->d_unit = device_get_unit(sc->dev);
182 disk_create(sc->disk, DISK_VERSION);
183 bioq_init(&sc->bio_queue);
184 kproc_create(&at45d_task, sc, &sc->p, 0, 0, "task: at45d flash");
186 config_intrhook_disestablish(&sc->config_intrhook);
190 at45d_open(struct disk *dp)
196 at45d_close(struct disk *dp)
202 at45d_strategy(struct bio *bp)
204 struct at45d_softc *sc;
206 sc = (struct at45d_softc *)bp->bio_disk->d_drv1;
208 bioq_disksort(&sc->bio_queue, bp);
214 at45d_task(void *arg)
216 struct at45d_softc *sc = (struct at45d_softc*)arg;
218 uint8_t txBuf[8], rxBuf[8];
219 struct spi_command cmd;
227 pdev = device_get_parent(dev);
230 bp = bioq_first(&sc->bio_queue);
232 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
233 } while (bp == NULL);
234 bioq_remove(&sc->bio_queue, bp);
236 sz = sc->disk->d_sectorsize;
237 end = bp->bio_pblkno + (bp->bio_bcount / sz);
238 for (block = bp->bio_pblkno; block < end; block++) {
239 char *vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
240 if (bp->bio_cmd == BIO_READ) {
241 txBuf[0] = CONTINUOUS_ARRAY_READ_HF;
245 txBuf[0] = PROGRAM_THROUGH_BUFFER;
249 // XXX only works on certain devices... Fixme
250 txBuf[1] = ((block >> 5) & 0xFF);
251 txBuf[2] = ((block << 3) & 0xF8);
260 err = SPIBUS_TRANSFER(pdev, dev, &cmd);
267 static devclass_t at45d_devclass;
269 static device_method_t at45d_methods[] = {
270 /* Device interface */
271 DEVMETHOD(device_probe, at45d_probe),
272 DEVMETHOD(device_attach, at45d_attach),
273 DEVMETHOD(device_detach, at45d_detach),
278 static driver_t at45d_driver = {
281 sizeof(struct at45d_softc),
284 DRIVER_MODULE(at45d, spibus, at45d_driver, at45d_devclass, 0, 0);