Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2000-2005, DENX Software Engineering |
| 4 | * Wolfgang Denk <wd@denx.de> |
| 5 | * Copyright (C) Procsys. All rights reserved. |
| 6 | * Mushtaq Khan <mushtaq_k@procsys.com> |
| 7 | * <mushtaqk_921@yahoo.co.in> |
| 8 | * Copyright (C) 2008 Freescale Semiconductor, Inc. |
| 9 | * Dave Liu <daveliu@freescale.com> |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Simon Glass | b8341f1 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 13 | #include <ahci.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 14 | #include <blk.h> |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 15 | #include <dm.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 16 | #include <part.h> |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 17 | #include <sata.h> |
| 18 | |
Simon Glass | b8341f1 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 19 | #ifndef CONFIG_AHCI |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 20 | struct blk_desc sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE]; |
Simon Glass | b8341f1 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 21 | #endif |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 22 | |
Simon Glass | b8341f1 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 23 | int sata_reset(struct udevice *dev) |
| 24 | { |
| 25 | struct ahci_ops *ops = ahci_get_ops(dev); |
| 26 | |
| 27 | if (!ops->reset) |
| 28 | return -ENOSYS; |
| 29 | |
| 30 | return ops->reset(dev); |
| 31 | } |
| 32 | |
| 33 | int sata_dm_port_status(struct udevice *dev, int port) |
| 34 | { |
| 35 | struct ahci_ops *ops = ahci_get_ops(dev); |
| 36 | |
| 37 | if (!ops->port_status) |
| 38 | return -ENOSYS; |
| 39 | |
| 40 | return ops->port_status(dev, port); |
| 41 | } |
| 42 | |
| 43 | int sata_scan(struct udevice *dev) |
| 44 | { |
| 45 | struct ahci_ops *ops = ahci_get_ops(dev); |
| 46 | |
| 47 | if (!ops->scan) |
| 48 | return -ENOSYS; |
| 49 | |
| 50 | return ops->scan(dev); |
| 51 | } |
| 52 | |
| 53 | #ifndef CONFIG_AHCI |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 54 | #ifdef CONFIG_PARTITIONS |
| 55 | struct blk_desc *sata_get_dev(int dev) |
| 56 | { |
| 57 | return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL; |
| 58 | } |
| 59 | #endif |
Simon Glass | b8341f1 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 60 | #endif |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 61 | |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 62 | #ifdef CONFIG_BLK |
| 63 | static unsigned long sata_bread(struct udevice *dev, lbaint_t start, |
| 64 | lbaint_t blkcnt, void *dst) |
| 65 | { |
| 66 | return -ENOSYS; |
| 67 | } |
| 68 | |
| 69 | static unsigned long sata_bwrite(struct udevice *dev, lbaint_t start, |
| 70 | lbaint_t blkcnt, const void *buffer) |
| 71 | { |
| 72 | return -ENOSYS; |
| 73 | } |
| 74 | #else |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 75 | static unsigned long sata_bread(struct blk_desc *block_dev, lbaint_t start, |
| 76 | lbaint_t blkcnt, void *dst) |
| 77 | { |
| 78 | return sata_read(block_dev->devnum, start, blkcnt, dst); |
| 79 | } |
| 80 | |
| 81 | static unsigned long sata_bwrite(struct blk_desc *block_dev, lbaint_t start, |
| 82 | lbaint_t blkcnt, const void *buffer) |
| 83 | { |
| 84 | return sata_write(block_dev->devnum, start, blkcnt, buffer); |
| 85 | } |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 86 | #endif |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 87 | |
Simon Glass | b8341f1 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 88 | #ifndef CONFIG_AHCI |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 89 | int __sata_initialize(void) |
| 90 | { |
Tang Yuantian | aa6ab90 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 91 | int rc, ret = -1; |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 92 | int i; |
| 93 | |
| 94 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) { |
| 95 | memset(&sata_dev_desc[i], 0, sizeof(struct blk_desc)); |
| 96 | sata_dev_desc[i].if_type = IF_TYPE_SATA; |
| 97 | sata_dev_desc[i].devnum = i; |
| 98 | sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN; |
| 99 | sata_dev_desc[i].type = DEV_TYPE_HARDDISK; |
| 100 | sata_dev_desc[i].lba = 0; |
| 101 | sata_dev_desc[i].blksz = 512; |
| 102 | sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz); |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 103 | #ifndef CONFIG_BLK |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 104 | sata_dev_desc[i].block_read = sata_bread; |
| 105 | sata_dev_desc[i].block_write = sata_bwrite; |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 106 | #endif |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 107 | rc = init_sata(i); |
| 108 | if (!rc) { |
| 109 | rc = scan_sata(i); |
| 110 | if (!rc && sata_dev_desc[i].lba > 0 && |
Tang Yuantian | aa6ab90 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 111 | sata_dev_desc[i].blksz > 0) { |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 112 | part_init(&sata_dev_desc[i]); |
Tang Yuantian | aa6ab90 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 113 | ret = i; |
| 114 | } |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Tang Yuantian | aa6ab90 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 118 | return ret; |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 119 | } |
| 120 | int sata_initialize(void) __attribute__((weak, alias("__sata_initialize"))); |
| 121 | |
| 122 | __weak int __sata_stop(void) |
| 123 | { |
| 124 | int i, err = 0; |
| 125 | |
| 126 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) |
| 127 | err |= reset_sata(i); |
| 128 | |
| 129 | if (err) |
| 130 | printf("Could not reset some SATA devices\n"); |
| 131 | |
| 132 | return err; |
| 133 | } |
| 134 | int sata_stop(void) __attribute__((weak, alias("__sata_stop"))); |
Simon Glass | b8341f1 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 135 | #endif |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 136 | |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 137 | #ifdef CONFIG_BLK |
| 138 | static const struct blk_ops sata_blk_ops = { |
| 139 | .read = sata_bread, |
| 140 | .write = sata_bwrite, |
| 141 | }; |
| 142 | |
| 143 | U_BOOT_DRIVER(sata_blk) = { |
| 144 | .name = "sata_blk", |
| 145 | .id = UCLASS_BLK, |
| 146 | .ops = &sata_blk_ops, |
| 147 | }; |
| 148 | #else |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 149 | U_BOOT_LEGACY_BLK(sata) = { |
| 150 | .if_typename = "sata", |
| 151 | .if_type = IF_TYPE_SATA, |
| 152 | .max_devs = CONFIG_SYS_SATA_MAX_DEVICE, |
| 153 | .desc = sata_dev_desc, |
| 154 | }; |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 155 | #endif |