ata_macio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright 2002 by Peter Grehan. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  24. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/cdefs.h>
  30. __FBSDID("$FreeBSD$");
  31. /*
  32. * Mac-io ATA controller
  33. */
  34. #include <sys/param.h>
  35. #include <sys/systm.h>
  36. #include <sys/kernel.h>
  37. #include <sys/module.h>
  38. #include <sys/bus.h>
  39. #include <sys/malloc.h>
  40. #include <sys/sema.h>
  41. #include <sys/taskqueue.h>
  42. #include <vm/uma.h>
  43. #include <machine/stdarg.h>
  44. #include <machine/resource.h>
  45. #include <machine/bus.h>
  46. #include <sys/rman.h>
  47. #include <sys/ata.h>
  48. #include <dev/ata/ata-all.h>
  49. #include <ata_if.h>
  50. #include <dev/ofw/ofw_bus.h>
  51. #include "ata_dbdma.h"
  52. /*
  53. * Offset to control registers from base
  54. */
  55. #define ATA_MACIO_ALTOFFSET 0x160
  56. /*
  57. * Define the gap between registers
  58. */
  59. #define ATA_MACIO_REGGAP 16
  60. /*
  61. * Whether or not to bind to the DBDMA IRQ
  62. */
  63. #define USE_DBDMA_IRQ 0
  64. /*
  65. * Timing register
  66. */
  67. #define ATA_MACIO_TIMINGREG 0x200
  68. #define ATA_TIME_TO_TICK(rev,time) howmany(time, (rev == 4) ? 15 : 30)
  69. #define PIO_REC_OFFSET 4
  70. #define PIO_REC_MIN 1
  71. #define PIO_ACT_MIN 1
  72. #define DMA_REC_OFFSET 1
  73. #define DMA_REC_MIN 1
  74. #define DMA_ACT_MIN 1
  75. struct ide_timings {
  76. int cycle; /* minimum cycle time [ns] */
  77. int active; /* minimum command active time [ns] */
  78. };
  79. static const struct ide_timings pio_timings[5] = {
  80. { 600, 180 }, /* PIO 0 */
  81. { 390, 150 }, /* PIO 1 */
  82. { 240, 105 }, /* PIO 2 */
  83. { 180, 90 }, /* PIO 3 */
  84. { 120, 75 } /* PIO 4 */
  85. };
  86. static const struct ide_timings dma_timings[3] = {
  87. { 480, 240 }, /* WDMA 0 */
  88. { 165, 90 }, /* WDMA 1 */
  89. { 120, 75 } /* WDMA 2 */
  90. };
  91. static const struct ide_timings udma_timings[5] = {
  92. { 120, 180 }, /* UDMA 0 */
  93. { 90, 150 }, /* UDMA 1 */
  94. { 60, 120 }, /* UDMA 2 */
  95. { 45, 90 }, /* UDMA 3 */
  96. { 30, 90 } /* UDMA 4 */
  97. };
  98. /*
  99. * Define the macio ata bus attachment.
  100. */
  101. static int ata_macio_probe(device_t dev);
  102. static int ata_macio_setmode(device_t dev, int target, int mode);
  103. static int ata_macio_attach(device_t dev);
  104. static int ata_macio_begin_transaction(struct ata_request *request);
  105. static int ata_macio_suspend(device_t dev);
  106. static int ata_macio_resume(device_t dev);
  107. static device_method_t ata_macio_methods[] = {
  108. /* Device interface */
  109. DEVMETHOD(device_probe, ata_macio_probe),
  110. DEVMETHOD(device_attach, ata_macio_attach),
  111. DEVMETHOD(device_suspend, ata_macio_suspend),
  112. DEVMETHOD(device_resume, ata_macio_resume),
  113. /* ATA interface */
  114. DEVMETHOD(ata_setmode, ata_macio_setmode),
  115. DEVMETHOD_END
  116. };
  117. struct ata_macio_softc {
  118. struct ata_dbdma_channel sc_ch;
  119. int rev;
  120. int max_mode;
  121. struct resource *sc_mem;
  122. uint32_t udmaconf[2];
  123. uint32_t wdmaconf[2];
  124. uint32_t pioconf[2];
  125. };
  126. static driver_t ata_macio_driver = {
  127. "ata",
  128. ata_macio_methods,
  129. sizeof(struct ata_macio_softc),
  130. };
  131. DRIVER_MODULE(ata, macio, ata_macio_driver, ata_devclass, NULL, NULL);
  132. MODULE_DEPEND(ata, ata, 1, 1, 1);
  133. static int
  134. ata_macio_probe(device_t dev)
  135. {
  136. const char *type = ofw_bus_get_type(dev);
  137. const char *name = ofw_bus_get_name(dev);
  138. struct ata_macio_softc *sc;
  139. if (strcmp(type, "ata") != 0 &&
  140. strcmp(type, "ide") != 0)
  141. return (ENXIO);
  142. sc = device_get_softc(dev);
  143. bzero(sc, sizeof(struct ata_macio_softc));
  144. if (strcmp(name,"ata-4") == 0) {
  145. device_set_desc(dev,"Apple MacIO Ultra ATA Controller");
  146. sc->rev = 4;
  147. sc->max_mode = ATA_UDMA4;
  148. } else {
  149. device_set_desc(dev,"Apple MacIO ATA Controller");
  150. sc->rev = 3;
  151. sc->max_mode = ATA_WDMA2;
  152. }
  153. return (ata_probe(dev));
  154. }
  155. static int
  156. ata_macio_attach(device_t dev)
  157. {
  158. struct ata_macio_softc *sc = device_get_softc(dev);
  159. uint32_t timingreg;
  160. struct ata_channel *ch;
  161. int rid, i;
  162. /*
  163. * Allocate resources
  164. */
  165. rid = 0;
  166. ch = &sc->sc_ch.sc_ch;
  167. sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
  168. RF_ACTIVE);
  169. if (sc->sc_mem == NULL) {
  170. device_printf(dev, "could not allocate memory\n");
  171. return (ENXIO);
  172. }
  173. /*
  174. * Set up the resource vectors
  175. */
  176. for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
  177. ch->r_io[i].res = sc->sc_mem;
  178. ch->r_io[i].offset = i * ATA_MACIO_REGGAP;
  179. }
  180. ch->r_io[ATA_CONTROL].res = sc->sc_mem;
  181. ch->r_io[ATA_CONTROL].offset = ATA_MACIO_ALTOFFSET;
  182. ata_default_registers(dev);
  183. ch->unit = 0;
  184. ch->flags |= ATA_USE_16BIT | ATA_NO_ATAPI_DMA;
  185. ata_generic_hw(dev);
  186. #if USE_DBDMA_IRQ
  187. int dbdma_irq_rid = 1;
  188. struct resource *dbdma_irq;
  189. void *cookie;
  190. #endif
  191. /* Init DMA engine */
  192. sc->sc_ch.dbdma_rid = 1;
  193. sc->sc_ch.dbdma_regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
  194. &sc->sc_ch.dbdma_rid, RF_ACTIVE);
  195. ata_dbdma_dmainit(dev);
  196. /* Configure initial timings */
  197. timingreg = bus_read_4(sc->sc_mem, ATA_MACIO_TIMINGREG);
  198. if (sc->rev == 4) {
  199. sc->udmaconf[0] = sc->udmaconf[1] = timingreg & 0x1ff00000;
  200. sc->wdmaconf[0] = sc->wdmaconf[1] = timingreg & 0x001ffc00;
  201. sc->pioconf[0] = sc->pioconf[1] = timingreg & 0x000003ff;
  202. } else {
  203. sc->udmaconf[0] = sc->udmaconf[1] = 0;
  204. sc->wdmaconf[0] = sc->wdmaconf[1] = timingreg & 0xfffff800;
  205. sc->pioconf[0] = sc->pioconf[1] = timingreg & 0x000007ff;
  206. }
  207. #if USE_DBDMA_IRQ
  208. /* Bind to DBDMA interrupt as well */
  209. if ((dbdma_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
  210. &dbdma_irq_rid, RF_SHAREABLE | RF_ACTIVE)) != NULL) {
  211. bus_setup_intr(dev, dbdma_irq, ATA_INTR_FLAGS, NULL,
  212. (driver_intr_t *)ata_interrupt, sc,&cookie);
  213. }
  214. #endif
  215. /* Set begin_transaction */
  216. sc->sc_ch.sc_ch.hw.begin_transaction = ata_macio_begin_transaction;
  217. return ata_attach(dev);
  218. }
  219. static int
  220. ata_macio_setmode(device_t dev, int target, int mode)
  221. {
  222. struct ata_macio_softc *sc = device_get_softc(dev);
  223. int min_cycle = 0, min_active = 0;
  224. int cycle_tick = 0, act_tick = 0, inact_tick = 0, half_tick;
  225. mode = min(mode, sc->max_mode);
  226. if ((mode & ATA_DMA_MASK) == ATA_UDMA0) {
  227. min_cycle = udma_timings[mode & ATA_MODE_MASK].cycle;
  228. min_active = udma_timings[mode & ATA_MODE_MASK].active;
  229. cycle_tick = ATA_TIME_TO_TICK(sc->rev,min_cycle);
  230. act_tick = ATA_TIME_TO_TICK(sc->rev,min_active);
  231. /* mask: 0x1ff00000 */
  232. sc->udmaconf[target] =
  233. (cycle_tick << 21) | (act_tick << 25) | 0x100000;
  234. } else if ((mode & ATA_DMA_MASK) == ATA_WDMA0) {
  235. min_cycle = dma_timings[mode & ATA_MODE_MASK].cycle;
  236. min_active = dma_timings[mode & ATA_MODE_MASK].active;
  237. cycle_tick = ATA_TIME_TO_TICK(sc->rev,min_cycle);
  238. act_tick = ATA_TIME_TO_TICK(sc->rev,min_active);
  239. if (sc->rev == 4) {
  240. inact_tick = cycle_tick - act_tick;
  241. /* mask: 0x001ffc00 */
  242. sc->wdmaconf[target] =
  243. (act_tick << 10) | (inact_tick << 15);
  244. } else {
  245. inact_tick = cycle_tick - act_tick - DMA_REC_OFFSET;
  246. if (inact_tick < DMA_REC_MIN)
  247. inact_tick = DMA_REC_MIN;
  248. half_tick = 0; /* XXX */
  249. /* mask: 0xfffff800 */
  250. sc->wdmaconf[target] = (half_tick << 21)
  251. | (inact_tick << 16) | (act_tick << 11);
  252. }
  253. } else {
  254. min_cycle =
  255. pio_timings[(mode & ATA_MODE_MASK) - ATA_PIO0].cycle;
  256. min_active =
  257. pio_timings[(mode & ATA_MODE_MASK) - ATA_PIO0].active;
  258. cycle_tick = ATA_TIME_TO_TICK(sc->rev,min_cycle);
  259. act_tick = ATA_TIME_TO_TICK(sc->rev,min_active);
  260. if (sc->rev == 4) {
  261. inact_tick = cycle_tick - act_tick;
  262. /* mask: 0x000003ff */
  263. sc->pioconf[target] =
  264. (inact_tick << 5) | act_tick;
  265. } else {
  266. if (act_tick < PIO_ACT_MIN)
  267. act_tick = PIO_ACT_MIN;
  268. inact_tick = cycle_tick - act_tick - PIO_REC_OFFSET;
  269. if (inact_tick < PIO_REC_MIN)
  270. inact_tick = PIO_REC_MIN;
  271. /* mask: 0x000007ff */
  272. sc->pioconf[target] =
  273. (inact_tick << 5) | act_tick;
  274. }
  275. }
  276. return (mode);
  277. }
  278. static int
  279. ata_macio_begin_transaction(struct ata_request *request)
  280. {
  281. struct ata_macio_softc *sc = device_get_softc(request->parent);
  282. bus_write_4(sc->sc_mem, ATA_MACIO_TIMINGREG,
  283. sc->udmaconf[request->unit] | sc->wdmaconf[request->unit]
  284. | sc->pioconf[request->unit]);
  285. return ata_begin_transaction(request);
  286. }
  287. static int
  288. ata_macio_suspend(device_t dev)
  289. {
  290. struct ata_dbdma_channel *ch = device_get_softc(dev);
  291. int error;
  292. if (!ch->sc_ch.attached)
  293. return (0);
  294. error = ata_suspend(dev);
  295. dbdma_save_state(ch->dbdma);
  296. return (error);
  297. }
  298. static int
  299. ata_macio_resume(device_t dev)
  300. {
  301. struct ata_dbdma_channel *ch = device_get_softc(dev);
  302. int error;
  303. if (!ch->sc_ch.attached)
  304. return (0);
  305. dbdma_restore_state(ch->dbdma);
  306. error = ata_resume(dev);
  307. return (error);
  308. }