Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 3665a40 | 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 | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Simon Glass | 821bb52 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 13 | #include <ahci.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 14 | #include <blk.h> |
Simon Glass | 7d741ac | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 15 | #include <dm.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 16 | #include <part.h> |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 17 | #include <sata.h> |
Tony Dinh | a87cbb8 | 2023-10-11 13:26:42 -0700 | [diff] [blame] | 18 | #include <dm/device-internal.h> |
| 19 | #include <dm/uclass-internal.h> |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 20 | |
Simon Glass | 821bb52 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 21 | #ifndef CONFIG_AHCI |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 22 | struct blk_desc sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE]; |
Simon Glass | 821bb52 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | int sata_reset(struct udevice *dev) |
| 26 | { |
| 27 | struct ahci_ops *ops = ahci_get_ops(dev); |
| 28 | |
| 29 | if (!ops->reset) |
| 30 | return -ENOSYS; |
| 31 | |
| 32 | return ops->reset(dev); |
| 33 | } |
| 34 | |
| 35 | int sata_dm_port_status(struct udevice *dev, int port) |
| 36 | { |
| 37 | struct ahci_ops *ops = ahci_get_ops(dev); |
| 38 | |
| 39 | if (!ops->port_status) |
| 40 | return -ENOSYS; |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 41 | |
Simon Glass | 821bb52 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 42 | return ops->port_status(dev, port); |
| 43 | } |
| 44 | |
| 45 | int sata_scan(struct udevice *dev) |
| 46 | { |
| 47 | struct ahci_ops *ops = ahci_get_ops(dev); |
| 48 | |
| 49 | if (!ops->scan) |
| 50 | return -ENOSYS; |
| 51 | |
| 52 | return ops->scan(dev); |
| 53 | } |
| 54 | |
Tony Dinh | a87cbb8 | 2023-10-11 13:26:42 -0700 | [diff] [blame] | 55 | int sata_rescan(bool verbose) |
| 56 | { |
| 57 | int ret; |
| 58 | struct udevice *dev; |
| 59 | |
| 60 | if (verbose) |
| 61 | printf("Removing devices on SATA bus...\n"); |
| 62 | |
| 63 | blk_unbind_all(UCLASS_AHCI); |
| 64 | |
| 65 | ret = uclass_find_first_device(UCLASS_AHCI, &dev); |
| 66 | if (ret || !dev) { |
| 67 | printf("Cannot find SATA device (err=%d)\n", ret); |
| 68 | return -ENOSYS; |
| 69 | } |
| 70 | |
| 71 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
| 72 | if (ret) { |
| 73 | printf("Cannot remove SATA device '%s' (err=%d)\n", dev->name, ret); |
| 74 | return -ENOSYS; |
| 75 | } |
| 76 | |
| 77 | if (verbose) |
| 78 | printf("Rescanning SATA bus for devices...\n"); |
| 79 | |
| 80 | ret = uclass_probe_all(UCLASS_AHCI); |
| 81 | |
Tony Dinh | e240edf | 2023-10-06 20:34:28 -0700 | [diff] [blame] | 82 | if (ret == -ENODEV) { |
| 83 | if (verbose) |
| 84 | printf("No SATA block device found\n"); |
| 85 | return 0; |
| 86 | } |
| 87 | |
Tony Dinh | a87cbb8 | 2023-10-11 13:26:42 -0700 | [diff] [blame] | 88 | return ret; |
| 89 | } |
| 90 | |
Simon Glass | 821bb52 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 91 | #ifndef CONFIG_AHCI |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 92 | #ifdef CONFIG_PARTITIONS |
| 93 | struct blk_desc *sata_get_dev(int dev) |
| 94 | { |
| 95 | return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL; |
| 96 | } |
| 97 | #endif |
Simon Glass | 821bb52 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 98 | #endif |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 99 | |
Simon Glass | 7d741ac | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 100 | static unsigned long sata_bread(struct udevice *dev, lbaint_t start, |
| 101 | lbaint_t blkcnt, void *dst) |
| 102 | { |
| 103 | return -ENOSYS; |
| 104 | } |
| 105 | |
| 106 | static unsigned long sata_bwrite(struct udevice *dev, lbaint_t start, |
| 107 | lbaint_t blkcnt, const void *buffer) |
| 108 | { |
| 109 | return -ENOSYS; |
| 110 | } |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 111 | |
Simon Glass | 821bb52 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 112 | #ifndef CONFIG_AHCI |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 113 | int __sata_initialize(void) |
| 114 | { |
Tang Yuantian | b450f93 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 115 | int rc, ret = -1; |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 116 | int i; |
| 117 | |
| 118 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) { |
| 119 | memset(&sata_dev_desc[i], 0, sizeof(struct blk_desc)); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 120 | sata_dev_desc[i].uclass_id = UCLASS_AHCI; |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 121 | sata_dev_desc[i].devnum = i; |
| 122 | sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN; |
| 123 | sata_dev_desc[i].type = DEV_TYPE_HARDDISK; |
| 124 | sata_dev_desc[i].lba = 0; |
| 125 | sata_dev_desc[i].blksz = 512; |
| 126 | sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz); |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 127 | rc = init_sata(i); |
| 128 | if (!rc) { |
| 129 | rc = scan_sata(i); |
| 130 | if (!rc && sata_dev_desc[i].lba > 0 && |
Tang Yuantian | b450f93 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 131 | sata_dev_desc[i].blksz > 0) { |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 132 | part_init(&sata_dev_desc[i]); |
Tang Yuantian | b450f93 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 133 | ret = i; |
| 134 | } |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Tang Yuantian | b450f93 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 138 | return ret; |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 139 | } |
| 140 | int sata_initialize(void) __attribute__((weak, alias("__sata_initialize"))); |
| 141 | |
| 142 | __weak int __sata_stop(void) |
| 143 | { |
| 144 | int i, err = 0; |
| 145 | |
| 146 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) |
| 147 | err |= reset_sata(i); |
| 148 | |
| 149 | if (err) |
| 150 | printf("Could not reset some SATA devices\n"); |
| 151 | |
| 152 | return err; |
| 153 | } |
| 154 | int sata_stop(void) __attribute__((weak, alias("__sata_stop"))); |
Simon Glass | 821bb52 | 2017-07-29 11:35:15 -0600 | [diff] [blame] | 155 | #endif |
Simon Glass | 3665a40 | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 156 | |
Simon Glass | 7d741ac | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 157 | static const struct blk_ops sata_blk_ops = { |
| 158 | .read = sata_bread, |
| 159 | .write = sata_bwrite, |
| 160 | }; |
| 161 | |
| 162 | U_BOOT_DRIVER(sata_blk) = { |
| 163 | .name = "sata_blk", |
| 164 | .id = UCLASS_BLK, |
| 165 | .ops = &sata_blk_ops, |
| 166 | }; |